hdu3314(Trouble with Election!)— 并查集

Trouble with Election!

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 390    Accepted Submission(s): 136


Problem Description
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  supporterswill 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.
 

Input
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.
 

Output
The output for every case consists of a single integer, which denotes that the M’th member is the winner or a single word, “Trouble”, if a unique winner can not be determined. Here quotes are for clarity.
 

Sample Input
  
  
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
 

Sample Output
  
  
2 3 Trouble Trouble Trouble
 
#include<stdio.h>
#include<string.h>
#define MAX 100000+10
int father[MAX];
int markl[MAX];
void init(int n){
    int i;
    for(i=0;i<n;i++){
        father[i]=i;
    }
}
int find(int x){
    while(x!=father[x]){
        x=father[x];
    }
    return x;
}
int vote[MAX];
void uni(int a,int b,int n){
    if(a!=b){
        int b1=find(b);
        if(a==b1){
            markl[b1]=1;
        }
        else{
            father[a]=b;
        }
    }
}
/*以后有这种返回值的问题一定要考虑是否在不同的情况下返回的是不同的值*/
void searchwin(int n){
    int i;
    int maxt=0;
    int marki=-1;
    int mark=0;
    for(i=0;i<n;i++){
        int t=find(i);
        if(!markl[t]){
			vote[t]++;
            if(maxt<vote[t]){
                 maxt=vote[t];
                 marki=t;
                 mark=1;
            }
            else if(maxt==vote[t]){
                mark=0;
            }
        }
    }
    if(mark==0||marki==-1)
		printf("Trouble\n");
    else//此时最好不要返回marki的值,因为marki很有可能会是0,之前我用了几种对的思路去做这道题,都是因为这个地方没有更正,所以wa。
        printf("%d\n",marki);
}
int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        memset(vote,0,sizeof(vote));
		memset(markl,0,sizeof(markl));
        init(n);
        int i;
        int select;
        for(i=0;i<n;i++){
            scanf("%d",&select);
            uni(i,select,n);
        }
		searchwin(n);
    }
	return 0;
}
!有时最好去测试一下边界值,这个题目思路虽然很好想,但是有的时候细节会害死人,有时候做题的时候,总觉得自己的思路解法没问题,往往是小地方导致错误
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值