algorithm/problem solving

acmicpc.net 2150(SCC), 4196(SCC)

qkqhxla1 2016. 10. 1. 14:03

https://www.acmicpc.net/problem/2150

#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAX = 10001;
 
int V, E, cnt, dfsn[MAX], SN, sn[MAX];
vector<int> adj[MAX];
bool finished[MAX]; 
stack<int> S;
vector<vector<int>> SCC;
 
int DFS(int curr){
    dfsn[curr] = ++cnt; 
    S.push(curr); 
 
    int result = dfsn[curr];
    for(int next: adj[curr]){
        if(dfsn[next] == 0) 
			result = min(result, DFS(next));
        else if(!finished[next]) 
			result = min(result, dfsn[next]);
    }
 
    if(result == dfsn[curr])
	{
        vector<int> currSCC;
        while(1)
		{
            int t = S.top();
            S.pop();
            currSCC.push_back(t);
            finished[t] = true;
            sn[t] = SN;
            if(t == curr) break;
        }
        sort(currSCC.begin(), currSCC.end());
        SCC.push_back(currSCC);
        SN++;
    }
    return result;
}
 
int main(){
    cin>>V>>E;
    for(int i=0; i<E; i++){
        int A, B;
        cin>>A>>B;
        adj[A].push_back(B);
    }
 
    for(int i=1; i<V+1; i++)
        if(dfsn[i] == 0) 
			DFS(i);
    sort(SCC.begin(), SCC.end());
 
    cout<<SN<<"\n";
	for(int i=0;i<SCC.size();i++)
	{
		for(int j=0;j<SCC[i].size();j++)
            cout<<SCC[i][j]<<" ";
        cout<<"-1\n";
    }
}

https://www.acmicpc.net/problem/4196


indegree가 0인걸 세면 된다. 처음엔 뭐야 간단하네? 하고 예전에 짜뒀던 위상정렬 코드에서


입력받고 indegree가 0인것만 가져와서 제출해봤는데 당연히 틀렸다. 


차이점은 여기서는 scc문제인만큼 scc를 만들어서 indegree를 세야 된다. 이거안하면


1->2->3->1같은 반복코드는 못셌었나 그랬다.

#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
const int MAX = 100001;
  
int V, E, cnt, dfsn[MAX], SN, sn[MAX];
vector<int> adj[MAX];
bool finished[MAX]; 
stack<int> S;
vector<vector<int>> SCC;
  
int DFS(int curr){
    dfsn[curr] = ++cnt; 
    S.push(curr); 
  
    int result = dfsn[curr];
    for(int next: adj[curr]){
        if(dfsn[next] == 0) 
            result = min(result, DFS(next));
        else if(!finished[next]) 
            result = min(result, dfsn[next]);
    }
  
    if(result == dfsn[curr])
    {
        vector<int> currSCC;
        while(1)
        {
            int t = S.top();
            S.pop();
            currSCC.push_back(t);
            finished[t] = true;
            sn[t] = SN;
            if(t == curr) break;
        }
        sort(currSCC.begin(), currSCC.end());
        SCC.push_back(currSCC);
        SN++;
    }
    return result;
}
  
int main(){
    int t;
    cin>>t;
    for(int k=0;k<t;k++)
    {
        cin>>V>>E;

		cnt = 0; SN = 0;
		memset(dfsn, 0, sizeof(dfsn));
        memset(sn, 0, sizeof(sn));
        memset(finished, 0, sizeof(finished));
		for(int i=1;i<V+1;i++)
			adj[i].clear();

        for(int i=0; i<E; i++){
            int A, B;
            cin>>A>>B;
            adj[A].push_back(B);
        }
  
        for(int i=1; i<V+1; i++)
            if(dfsn[i] == 0) 
                DFS(i);
 
        int indegree[MAX]={0,};
        for(int i=1;i<V+1;i++)
            for(int j=0;j<adj[i].size();j++)
                if(sn[i]!=sn[adj[i][j]])
                    indegree[sn[adj[i][j]]]++;
             
        int answer = 0;
        for(int i=0;i<SN;i++)
        {
            if(indegree[i]==0)
                answer++;
        }
        cout<<answer<<"\n";
    }
}