首页 > ACM题库 > HDU-杭电 > HDU 3314-Trouble with Election!-并查集-[解题报告]HOJ
2014
03-16

HDU 3314-Trouble with Election!-并查集-[解题报告]HOJ

Trouble with Election!

问题描述 :

Taman and his friends are going to arrange an election for their club. The rule of this election is simple. Every member of the club can cast a single vote for one person. Even one can vote for himself too. If A casts his vote for B and B casts his vote for C then it means that C gets both the votes of A and B. It means if a man casts his vote for someone then the votes he got will also be added to the person he votes for. Now there is a problem if A’s vote goes for B and B’s vote for A. In this case, both of them have the same number of votes and it is a tie! So a tiebreaker will be needed. In this situation, all the votes of A and B and their supporters will be cancelled. If one’s vote goes for another then one is considered as the supporter of another.
Now you are elected as the election commissioner for the election of the club. It is your duty to publish the result of the election. If you find no possible winner or if two or more members have same number of votes then you should declare the situation as “Trouble”, print the name of the winner otherwise. Instead of name, a unique number for each member identifies the members of this club and the number should always be non-negative and less than the total number of members of the club.

输入:

There will be multiple test cases. Every test case starts with a single integer N on a line. 0<N<= 100000. N denotes the number of members of the club. N lines following. Each I’th line will be consisting of a single integer J; denoting I’th member casts his vote for J’th member. 0<=I, J<N.

输出:

There will be multiple test cases. Every test case starts with a single integer N on a line. 0<N<= 100000. N denotes the number of members of the club. N lines following. Each I’th line will be consisting of a single integer J; denoting I’th member casts his vote for J’th member. 0<=I, J<N.

样例输入:

3
1
2
2
4
1
2
0
3
2
1
0
5
1
2
0
2
3
10
7
6
1
8
9
2
7
9
5
4

样例输出:

2
3
Trouble
Trouble
Trouble

这题想通了就好做了,只有投了自己的人才可能是最后的WINNER,找符合的人中最大值是不是唯一

#include<iostream>
using namespace std;
const int maxn=100000+10;
int p[maxn],r[maxn],cnt[maxn];
void make()
{
    memset(r,0,sizeof(r));
    memset(p,255,sizeof(p));
    for(int i=0;i<maxn;i++) cnt[i]=1;
}
int find(int x)
{
    int px,t;
    for(px=x;p[px]>=0;px=p[px]);
    while (x!=px) {
        t=p[x];
        p[x]=px;
        x=t;
    }
    return px;
}
int unio(int x,int y)
{
    x=find(x),y=find(y);
    if(x==y)    return -1;
    if(r[x]>r[y])
    {
        p[y]=x;
        cnt[y]+=cnt[x];
        cnt[x]=cnt[y];
        return x;
    }
    else
    {
        p[x]=y;
        cnt[x]+=cnt[y];
        cnt[y]=cnt[x];
        if(r[x]==r[y])  r[y]++;
        return y;
    }
}
int d[maxn];
int main()
{
    int n;
    while (scanf("%d",&n)!=EOF)
    {
        make();
        for(int i=0;i<n;i++){
            scanf("%d",&d[i]);
            unio(i,d[i]);
        }
        int tmp_max=-1,k=-1;
        bool flag=false;
        for(int i=0;i<n;i++)
        {
            if(i==d[i])
            {
                if(tmp_max==cnt[find(i)])
                {
                    flag=false;
                }
                if(cnt[find(i)]>tmp_max)
                {
                    flag=true;
                    tmp_max=cnt[find(i)];
                    k=i;
                }
            }

        }
        if(!flag)
        {
            printf("Trouble\n");
        }
        else
        {
            printf("%d\n",k);
        }
    }
    return 0;
}

参考:http://blog.csdn.net/wxfwxf328/article/details/7213568


  1. #!/usr/bin/env python
    def cou(n):
    arr =
    i = 1
    while(i<n):
    arr.append(arr[i-1]+selfcount(i))
    i+=1
    return arr[n-1]

    def selfcount(n):
    count = 0
    while(n):
    if n%10 == 1:
    count += 1
    n /= 10
    return count

  2. 如果两个序列的最后字符不匹配(即X [M-1]!= Y [N-1])
    L(X [0 .. M-1],Y [0 .. N-1])= MAX(L(X [0 .. M-2],Y [0 .. N-1]),L(X [0 .. M-1],Y [0 .. N-1])
    这里写错了吧。