UVA10227 POJ2419 ZOJ1900 Forests【并查集+set】

If a tree falls in the forest, and there’s nobody there to hear, does it make a sound? This classic conundrum was coined by George Berkeley (1685-1753), the Bishop and influential Irish philosopher whose primary philosophical achievement is the advancement of what has come to be called subjective idealism. He wrote a number of works, of which the most widelyread are Treatise Concerning the Principles of Human Knowledge (1710) and Three Dialogues between Hylas and Philonous (1713) (Philonous, the “lover of the mind”, representing Berkeley himself).
在这里插入图片描述
    A forest contains T trees numbered from 1 to T and P people numbered from 1 to P.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
    Standard input consists of a line containing P and T followed by several lines, containing a pair of integers i and j, indicating that person i has heard tree j fall. People may have different opinions as to which trees, according to Berkeley, have made a sound.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
    How many different opinions are represented in the input? Two people hold the same opinion only if they hear exactly the same set of trees. You may assume that P < 100 and T < 100.
Sample Input
1

3 4
1 2
3 3
1 3
2 2
3 2
2 4
Sample Output
2

问题链接UVA11559 Event Planning
问题简述:(略)
问题分析
    有P个人和T棵树。给定某人听到某棵树倒下声音的若干二元组,若两个人听到的树的集合相同则他们同属于一个集合,问人分成几个集合。
    有2种解法,一是用并查集,二是用STL的set。
程序说明
    POJ和ZOJ与UVA的题是一样的,输入输出格式不同。
参考链接:(略)
题记:(略)

AC的C++语言程序(POJ)如下:

/* POJ2419 Forests */

#include <iostream>
#include <set>

using namespace std;

const int N = 100 + 1;

int main()
{
    int p, t, a, b;
    cin >> p >> t;
    set<int> s[N];
    while(cin >> a >> b)
        s[a].insert(b);

    set<set<int> > s2(s + 1, s + p + 1);

    cout << s2.size() << endl;

    return 0;
}

AC的C++语言程序(UVA)如下:

/* UVA10227 Forests */

#include <bits/stdc++.h>

using namespace std;

const int N = 100 + 1;

int main()
{
    int n, p, t, a, b;
    string line;
    cin >> n;
    while(n--) {
        set<int> s[N];     // 观点

        cin >> p >> t;
        cin.ignore();
        while(getline(cin, line) && !line.empty()) {
            stringstream ss(line);
            ss >> a >> b;
            s[a].insert(b);
        }

        set<set<int> > s2;
        for(int i = 1; i <= p; i++)
            s2.insert(s[i]);

        cout << s2.size() << endl;
        if(n) cout << endl;
    }

    return 0;
}

AC的C++语言程序(UVA)如下:

/* UVA10227 Forests */

#include <bits/stdc++.h>

using namespace std;

const int N = 100 + 1;
int f[N];

void UFInit(int n)
{
    for(int i = 0; i < n; i++)
        f[i] = i;
}

int Find(int a) {
    return a == f[a] ? a : f[a] = Find(f[a]);
}

void Union(int a, int b)
{
    a = Find(a);
    b = Find(b);
    if (a != b) {
        f[a] = b;
    }
}

const int M = 128;
char buf[128];
int p2t[N][N], vis[N];

bool judge(int p1, int p2, int t)
{
    for(int i = 1; i <= t; i++)
        if(p2t[p1][i] != p2t[p2][i])
            return false;
    return true;
}

int main()
{
    int n, p, t, a, b;
    scanf("%d", &n);
    while(n--) {
        UFInit(N);

        memset(vis, 0, sizeof(vis));

        scanf("%d%d", &p, &t);
        getchar();
        while(fgets(buf, M, stdin) && buf[0] != '\n') {
            sscanf(buf, "%d%d", &a, &b);
            vis[a] = 1;
            p2t[a][b] = 1;
        }

        for(int i = 1; i <= p; i++)
            for(int j = i; j <= p; j++)
                if(vis[i] && vis[j] && judge(i, j, t))
                    Union(i, j);

        int cnt = 0;
        for(int i = 0; i <= p; i++)
            if(vis[i] && f[i] == i) cnt++;
        printf("%d\n", cnt);
        if(n) printf("\n");
    }

    return 0;
}

AC的C++语言程序(ZOJ,POJ出现WA)如下:

/* ZOJ1900 POJ2419 Forests */

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

const int N = 100;
int f[N];

void UFInit(int n)
{
    for(int i = 0; i < n; i++)
        f[i] = i;
}

int Find(int a) {
    return a == f[a] ? a : f[a] = Find(f[a]);
}

void Union(int a, int b)
{
    a = Find(a);
    b = Find(b);
    if (a != b) {
        f[a] = b;
    }
}

const int M = 128;
char buf[128];
int p2t[N][N], vis[N];

bool judge(int p1, int p2, int t)
{
    for(int i = 1; i <= t; i++)
        if(p2t[p1][i] != p2t[p2][i])
            return false;
    return true;
}

int main()
{
    int p, t, a, b;
    while(~scanf("%d%d", &p, &t)) {
        getchar();
        UFInit(N);

        memset(vis, 0, sizeof(vis));
        while(fgets(buf, M, stdin) && buf[0] != '\n') {
            sscanf(buf, "%d%d", &a, &b);
            vis[a] = 1;
            p2t[a][b] = 1;
        }

        for(int i = 1; i <= p; i++)
            for(int j = i; j <= p; j++)
                if(vis[i] && vis[j] && judge(i, j, t))
                    Union(i, j);

        int cnt = 0;
        for(int i = 0; i <= p; i++)
            if(vis[i] && f[i] == i) cnt++;
        printf("%d\n", cnt);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/89944289