编程练习9(图的连通性)

A:Popular Cows(POJ 2186)

Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu

Description

Every cow’s dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow.

Input

  • Line 1: Two space-separated integers, N and M

  • Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.

Output

  • Line 1: A single integer that is the number of cows who are considered popular by every other cow.

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity.

我的代码

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
int T,n,m;
int x, y;
vector <int> v[10005];
//DFN:DFS number
//f:属于哪个强连通分量
//cnt:强连通分量标号
int DFN[10005], low[10005], time_clock = 0, top = 0, c[10005], f[10005], cnt = 0, sum[10005], out0[10005];
void tarjan(int u){
    low[u] = DFN[u] = ++time_clock;
    c[++top] = u;
    for (int i = 0; i < v[u].size(); i++) {
        int vector = v[u][i];
        if (!DFN[vector]) {
            tarjan(vector);
            low[u] = min(low[u], low[vector]);
        }else if (!f[vector]){
            low[u] = min(low[u], DFN[vector]);
        }
    }
    if ( low[u] == DFN[u]) {
        int len = top;
        cnt ++;
        while( c[top] != u){
            f[c[top--]] = cnt;
        }
        f[c[top--]] = cnt;
        sum[cnt] = len - top;
    }
}

void init(){
    memset(DFN, 0, sizeof(DFN));
    memset(low, 0, sizeof(low));
    memset(c, 0, sizeof(c));
    memset(f, 0, sizeof(f));
    memset(sum, 0, sizeof(sum));
    memset(out0, 0, sizeof(out0));
    time_clock = 0;
    top = 0;
    cnt = 0;
    for (int i = 1; i <= n ; i++) {
        v[i].clear();
    }

}
int main() {
    //freopen("input.txt", "r", stdin);
    int i, j;
    while(scanf("%d%d",&n,&m) != EOF) {
        init();
        for (i = 0; i < m; i++) {
            scanf("%d%d", &x ,&y);
            v[x].push_back(y);
        }
        for (i = 1; i <= n; i++) {
            if (!DFN[i]) {
                tarjan(i);
            }
        }
        for (i = 1; i <= n; i++) {
            for (j = 0; j < v[i].size(); j++) {
                if (f[i] != f[v[i][j]]) {
                    out0[f[i]] ++;
                }
            }
        }
        int x = 0;
        int num = 0;
        for (i = 1; i <=cnt; i++) {
            if (!out0[i]) {
                x++;
                num = i;
            }
        }
        if (x == 1) {
            printf("%d",sum[num]);
        }else{
            printf("0");
        }
        /*for(i=1;i<=cnt;++i)
        if(!out0[i]){
            if(x>0){
                printf("0");
                return 0;
            }
            x=sum[i];
        }
        printf("%d",x);*/
    }
    return 0;
}

B:Road Construction(POJ 3352)

Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu

Description

It’s almost summer time, and that means that it’s almost summer construction time! This year, the good people who are in charge of the roads on the tropical island paradise of Remote Island would like to repair and upgrade the various roads that lead between the various tourist attractions on the island.

The roads themselves are also rather interesting. Due to the strange customs of the island, the roads are arranged so that they never meet at intersections, but rather pass over or under each other using bridges and tunnels. In this way, each road runs between two specific tourist attractions, so that the tourists do not become irreparably lost.

Unfortunately, given the nature of the repairs and upgrades needed on each road, when the construction company works on a particular road, it is unusable in either direction. This could cause a problem if it becomes impossible to travel between two tourist attractions, even if the construction company works on only one road at any particular time.

So, the Road Department of Remote Island has decided to call upon your consulting services to help remedy this problem. It has been decided that new roads will have to be built between the various attractions in such a way that in the final configuration, if any one road is undergoing construction, it would still be possible to travel between any two tourist attractions using the remaining roads. Your task is to find the minimum number of new roads necessary.

Input

The first line of input will consist of positive integers n and r, separated by a space, where 3 ≤ n ≤ 1000 is the number of tourist attractions on the island, and 2 ≤ r ≤ 1000 is the number of roads. The tourist attractions are conveniently labelled from 1 to n. Each of the following r lines will consist of two integers, v and w, separated by a space, indicating that a road exists between the attractions labelled v and w. Note that you may travel in either direction down each road, and any pair of tourist attractions will have at most one road directly between them. Also, you are assured that in the current configuration, it is possible to travel between any two tourist attractions.

Output

One line, consisting of an integer, which gives the minimum number of roads that we need to add.

Sample Input

Sample Input 1
10 12
1 2
1 3
1 4
2 5
2 6
5 6
3 7
3 8
7 8
4 9
4 10
9 10

Sample Input 2
3 3
1 2
2 3
1 3

Sample Output

Output for Sample Input 1
2

Output for Sample Input 2
0

我的代码

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
int T,n,m;
int x, y;
vector <int> v[1005];
//DFN:DFS number
//f:属于哪个强连通分量
//cnt:强连通分量标号
int DFN[1005], low[1005], time_clock = 0, top = 0, c[1005], f[1005], cnt = 0, sum[1005], out0[1005];
int state[1005];
void tarjan(int u, int fa){
    state[u] = 1;
    low[u] = DFN[u] = ++time_clock;
    for (int i = 0; i < v[u].size(); i++) {
        int vector = v[u][i];
        if (state[vector] == 0) {
            tarjan(vector, u);
            low[u] = min(low[u], low[vector]);
        }else if (state[vector] == 1 && vector != fa){
            low[u] = min(low[u], DFN[vector]);
        }
    }
    state[u] = 2;
}

void init(){
    memset(state, 0, sizeof(state));
    memset(DFN, 0, sizeof(DFN));
    memset(low, 0, sizeof(low));
    memset(c, 0, sizeof(c));
    memset(f, 0, sizeof(f));
    memset(sum, 0, sizeof(sum));
    memset(out0, 0, sizeof(out0));
    time_clock = 0;
    top = 0;
    cnt = 0;
    for (int i = 1; i <= n; i++) {
        v[i].clear();
    }

}
int main() {
    //freopen("input.txt","r", stdin);
    int i, j;
    while(scanf("%d%d",&n,&m) != EOF) {
        init();
        for (i = 0; i < m; i++) {
            scanf("%d%d", &x ,&y);
            v[x].push_back(y);
            v[y].push_back(x);
        }
        tarjan(1, 1);
        for (i = 1; i <= n; i++) {
            for (j = 0; j < v[i].size(); j++) {
                int vector = v[i][j];
                if (low[i] != low[vector]) {
                    out0[low[i]] ++;
                }
            }
        }
        int leaf = 0;
        for (i = 1; i <=n; i++) {
            if (out0[i] == 1) {
                leaf++;
            }
        }
        printf("%d\n",(leaf+1)/2);
    }
    return 0;
}

你可能感兴趣的:(问题求解)