poj2912 带权并查集,类似食物链
来源:互联网 发布:二维数组回形遍历 编辑:程序博客网 时间:2024/05/16 02:42
真是看不懂题目,看题解才知道它要干嘛
枚举裁判。。
如果只有一个裁判,输出确定他人不是裁判所需最大行数,用数组error记录
如果没有裁判输出impossible,多个裁判不确定
注意unionset推的关系
#include <iostream>#include<stdio.h>#include<math.h>#include<algorithm>#include<cstring>#include<string>#include<vector>#include<queue>#include<map>#include<set>#include<stack>using namespace std;#define FOR(i,j,k) for(int i=j;i<=k;i++)#define mod 1000000007#define N 510int f[N];int r[N];int error[N];struct node{ int u,v,w;}edge[2010];int n;int find(int x){ if(x==f[x])return f[x]; int t=f[x]; f[x]=find(f[x]); r[x]=(r[t]+r[x])%3; return f[x];}bool unionset(int a,int b,int w){ int aa=find(a),bb=find(b); if(aa==bb) { if((r[a]+w)%3==r[b]) return true; else return false; } f[bb]=aa; r[bb]=(r[a]+w-r[b]+3)%3; return true;}void init(){ FOR(i,1,n) f[i]=i; memset(r,0,sizeof(r));}int main(){ int m; while(scanf("%d%d",&n,&m)!=EOF) { char op; FOR(i,1,m) { scanf("%d%c%d",&edge[i].u,&op,&edge[i].v); edge[i].u++,edge[i].v++; if(op=='=') edge[i].w=0; if(op=='<') edge[i].w=1; if(op=='>') edge[i].w=2; } memset(error,0,sizeof(error)); FOR(i,1,n) //枚举i为裁判 { init(); FOR(j,1,m) { if(edge[j].u==i||edge[j].v==i) continue; if(unionset(edge[j].u,edge[j].v,edge[j].w)==false) { error[i]=j; break; } } } int ans=0; int ans2=0; int shu=0; FOR(i,1,n) { //cout<<i<<" "<<error[i]<<endl; if(error[i]==0) ans=i,shu++; ans2=max(ans2,error[i]); } ans--; if(shu>1) printf("Can not determine\n"); if(shu==0) printf("Impossible\n"); if(shu==1) printf("Player %d can be determined to be the judge after %d lines\n",ans,ans2); } return 0;}
0 0
- poj2912 带权并查集,类似食物链
- poj2912(带权并查集)
- POJ2912-并查集
- poj1182食物链(带权并查集)
- 食物链(带权并查集)
- poj1182(食物链)----带权并查集
- 【NOIP2001】食物链【带权并查集】
- POJ1182-食物链(带权并查集)
- poj1182 食物链【带权并查集】
- 食物链 带权并查集
- 食物链(带权并查集)
- POJ1182 食物链 带权并查集
- 食物链 带权并查集
- POJ1182 食物链 (带权并查集)
- 食物链(带权并查集)
- poj1182 食物链 (带权并查集)
- 并查集-POJ2912 Rochambeau
- 【POJ2912】【加权并查集】【食物链变形】【只有一个就是只有他能nobug】
- 将汉字转化为拼音
- cocoapods的安装,使用,卸载,以及你可能会遇到的坑..
- 【JAVA程序】猜拳游戏
- docker swarm集群TLS配置
- WCF 配置服务 (02)
- poj2912 带权并查集,类似食物链
- Android之——图片的内存优化
- C语言05 -- 二维数组
- 简说mysql分表分区
- QT中QWidget、QDialog及QMainWindow的区别
- mac自动补全和高亮
- 关于读取CocoStudio的ui文件可能遇到的问题
- 使用myeclipse启动tomcat时出现这种错误“Could not publish to the server. java.lang.NullPointerException”
- 字符串中的子串替换