csu1904——精灵的交际网

来源:互联网 发布:矩阵音响效果图 编辑:程序博客网 时间:2024/05/22 21:16
天才少女科学家Cicini正在研究某种罕见的精灵的交际行为。她认为这些精灵只会与自己性别相反的精灵发生谈话,这些精灵一共也只有两种性别。在她的实验之中,由于每一个精灵的翅膀上都被写上了序号,因此每一个个体都是可以相互区分的。但是由于她的女仆Syaro带着Cicini和Cicini的朋友们一起开心的度过了万圣节,导致Cicini的实验报告在某一条实验记录后开始发生错误了,现在请你找出一定会出现性别冲突的第一条错误的实验记录出现在哪里。
Input

第一行是一个数据组数t,对于每一组数据,第一行是有两个整数n和k,(n,k<=50000) ,表示在这一次实验中一共使用了n只精灵以及有k条实验记录,接下来k行,每一行有两个正整数数a和b,表示精灵a和b发生了谈话。(a,b<=n , a!=b)
Output

对于每一组数据,输出第一条错误的实验记录的位置,假若到最后都未发现错误,则输出-1
Sample Input

2
3 3
1 2
2 3
1 3
4 2
1 2
3 4

Sample Output

3

-1

解题思路:并查集的模板题。

#include<iostream>#include<algorithm>#include<cstdio>using namespace std;const int MAXN=50005;int f[MAXN],r[MAXN];void init_set(int n){    for(int i=0; i<=n; i++)        f[i]=i,r[i]=0;}int find_set(int v){    if(v==f[v]) return v;    else    {        int rt=find_set(f[v]);        r[v]=r[v]^r[f[v]];        return f[v]=rt;    }}void union_set(int x,int y){    int fx=find_set(x);    int fy=find_set(y);    f[fx]=fy;    r[fx]=(!(r[x]^r[y]));}int main(){    int T;    cin>>T;    while(T--)    {        int n,m;        cin>>n>>m;        init_set(n);        int x,y;        bool flag=false;        int cnt=0;        for(int i=1; i<=m; i++)        {            cin>>x>>y;            int fx=find_set(x),fy=find_set(y);            if(flag) continue;            if(fx==fy&&r[x]==r[y])            {                flag=true;                cnt=i;            }            if(fx!=fy)  union_set(x,y);        }        if(flag)  cout<<cnt<<endl;        else  cout<<"-1"<<endl;    }    return 0;}