hdu2767:强连通分量

有向图,求最少增加多少边能够使整个图成为一个连通图,tarjan缩点,然后max(出度为0的点,入度为0的点),原理YY一下就懂了。

-------------------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stack>
#include<vector>
#include<queue>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;i++)
#define clr(x,c) memset(x,c,sizeof(x))
const int nmax=20005;
int read(){
    int x=0,f=1;char c=getchar();
    while(!isdigit(c)){
        if(c=='-') f=-1;
        c=getchar();
    }
    while(isdigit(c)){
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}
struct edge{
    int to;
    edge *next;
};
int n,m,ss,t,scc_cnt,dfn_clock;
stack<int>s;
edge e[50005],*head[nmax],*pt;
int pre[nmax],low[nmax],sccno[nmax],l[nmax],r[nmax];
void adde(int u,int v){
    pt->to=v;pt->next=head[u];head[u]=pt++;
}
void dfs(int x){
    pre[x]=low[x]=++dfn_clock;
    s.push(x);
    for(edge *ee=head[x];ee;ee=ee->next){
        int to=ee->to;
        if(!pre[to]){
            dfs(to);
            low[x]=min(low[x],low[to]);
        }else if(!sccno[to])
            low[x]=min(low[x],pre[to]);
    }
    if(low[x]==pre[x]){
        scc_cnt++;
        while(1){
            int u=s.top();s.pop();
            sccno[u]=scc_cnt;
            if(x==u) break;
        }
    }
    return ;
}
void init(){
    pt=e;
    clr(pre,0);clr(head,0);clr(sccno,0);dfn_clock=0;scc_cnt=0;clr(l,0);clr(r,0);
}
int main(){
    int k=read();
    while(k--){
        init();
        n=read(),m=read();
        rep(i,m){
            ss=read(),t=read();
            adde(ss,t);
        }
        rep(i,n){
            if(!pre[i]) dfs(i);
        }
        rep(i,n){
            for(edge *ee=head[i];ee;ee=ee->next){
                if(sccno[i]==sccno[ee->to]) continue;
                r[sccno[i]]++;
                l[sccno[ee->to]]++;
            }
        }
        if(scc_cnt==1){
            printf("0\n");
            continue;
        }
        int lsum=0,rsum=0;
        rep(i,scc_cnt){
            if(l[i]==0) lsum++;
            if(r[i]==0) rsum++;
        }
        printf("%d\n",max(lsum,rsum));
    }
    return 0;
}

-------------------------------------------------------------------------------------------

 

Proving Equivalences
Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

Submit Status

Description

Consider the following exercise, found in a generic linear algebra textbook. 

Let A be an n × n matrix. Prove that the following statements are equivalent: 

1. A is invertible. 
2. Ax = b has exactly one solution for every n × 1 matrix b. 
3. Ax = b is consistent for every n × 1 matrix b. 
4. Ax = 0 has only the trivial solution x = 0. 

The typical way to solve such an exercise is to show a series of implications. For instance, one can proceed by showing that (a) implies (b), that (b) implies (c), that (c) implies (d), and finally that (d) implies (a). These four implications show that the four statements are equivalent. 

Another way would be to show that (a) is equivalent to (b) (by proving that (a) implies (b) and that (b) implies (a)), that (b) is equivalent to (c), and that (c) is equivalent to (d). However, this way requires proving six implications, which is clearly a lot more work than just proving four implications! 

I have been given some similar tasks, and have already started proving some implications. Now I wonder, how many more implications do I have to prove? Can you help me determine this?
 

Input

On the first line one positive number: the number of testcases, at most 100. After that per testcase: 

* One line containing two integers n (1 ≤ n ≤ 20000) and m (0 ≤ m ≤ 50000): the number of statements and the number of implications that have already been proved. 
* m lines with two integers s1 and s2 (1 ≤ s1, s2 ≤ n and s1 ≠ s2) each, indicating that it has been proved that statement s1 implies statement s2.
 

Output

Per testcase: 

* One line with the minimum number of additional implications that need to be proved in order to prove that all statements are equivalent.
 

Sample Input

2 4 0 3 2 1 2 1 3
 

Sample Output

4 2
 

Source

NWERC 2008

Submit Status

你可能感兴趣的:(hdu2767:强连通分量)