POJ 1703 Find them, Catch them
来源:互联网 发布:qt淘宝互刷流量 编辑:程序博客网 时间:2024/05/10 16:31
题目大意:
Tadu城的警察想和混乱说拜拜,准备行动起来铲除城中的两大黑帮——龙帮和蛇帮,但首先他们得弄清楚罪犯到底属于哪个帮才能准确定罪,现在的问题是给你两个罪犯,你能否判断他们两是否属于同一帮。
现有多个测例(测例数题中会给出),假设每个测例中会告诉你罪犯总共有N个(N ≤ 100,000,其编号为1-N),每个罪犯只属于两个帮派中的一个,接下来会有M条信息给你处理:
D a b
表示a和b属于不同帮派;
A a b
让你给出判断,判断a和b是否属于同一帮派(也有可能无法判断,依据前面给出的信息来判断);
题目链接
思路一:将D信息中的两个罪犯合并在同一集合中,表示(a, b)对被考察过了,然后利用各自和并查集根的关系判断他们是否在同一帮派中。
注释代码:
/* * Problem ID : POJ 1703 Find them, Catch them * Author : Lirx.t.Una * Language : C++ * Run Time : 297 ms * Run Memory : 616 KB */ #include <string.h>#include <stdio.h>//maximum number of criminals,罪犯的最大数量//100,000 + 1#defineMAXN100001#defineTRUE1#defineFALSE0intset[MAXN];//考查集合,将所有考查过的罪犯纳入一个集合charr[MAXN];//relationship,每个罪犯和根结点的关系 //若和根结点在同一帮派则为TRUE,否则为FALSEintfind(int x) {if ( x == set[x] )return x;intf;f= set[x];//保存x的老根set[x]= find(f);//对x路径压缩//由于x被路径压缩了,所以其根结点改变了//接在新的根结点上后需要更新它和新根的关系//新关系可以由它跟老根的关系和老根和新根的关系推出 //(因为这是递归进行的,因此老根在上面递归结束后必然接在了现在的新根上, //并且它跟新根的关系已经递归地更新过了,所以在这一层上可以放心利用老根和 //新根的关系)//若它俩关系相同那么必然x和新根在同一帮派,否则就处于不同帮派r[x] = !( r[x] ^ r[f] );return set[x];}intmain() {intt;//测例数intn;//罪犯总数intm;//信息总数inti;//计数变量charcmd;//信息中的命令部分inta, b;//信息中的两个罪犯的编号intsa, sb;//set of a and b,a和b的根结点(即所属的集合)scanf("%d", &t);while ( t-- ) {scanf("%d%d", &n, &m);//由于初始化时每个罪犯的信息未知,所以每个人都归到以自己//为编号的集合中,因此自己就是根结点,自己和根结点必然都//同属于一个帮派,所以都初始化为TRUEmemset(r + 1, TRUE, n * sizeof(char));//并查集初始化for ( i = 1; i <= n; i++ )set[i] = i;while ( m-- ) {scanf("\n%c%d%d", &cmd, &a, &b);sa = find(a);sb = find(b);switch (cmd) {case 'A' :if ( sa != sb ) {//代表a、b对还没有考察过puts("Not sure yet.");break;}if ( r[a] == r[b] ) {//现a、b同属一个集合了 //若a、b和各自的根(两根相同)的关系 //相同,则他俩必定在同一帮派中puts("In the same gang.");break;}puts("In different gangs.");//否则必定不在一个帮派中break;default ://若为Dif ( sa != sb ) {//a、b不在一个集合中 //这就说明a、b对还没有被考察过//以下对a、b两个所属的集合进行合并set[sa] = sb;//sa的根发生变化(本来sa的根就是自己) //sa换了新根后需要对其和新根的关系进行更新//该关系可以从a和b的关系,以及a、b各自的老根的关系推出//由于a和b必定不在一个帮派中//所以如果r[a]、r[b]相同,则r[sa]必定为FALSE,否则为TRUEr[sa] = !!( r[a] ^ r[b] );break;}}}}return 0;}无注释代码:
#include <string.h>#include <stdio.h>#defineMAXN100001#defineTRUE1#defineFALSE0intset[MAXN];charr[MAXN];intfind(int x) {if ( x == set[x] )return x;intf;f= set[x];set[x]= find(f);r[x] = !( r[x] ^ r[f] );return set[x];}intmain() {intt;intn;intm;inti;charcmd;inta;intb;intsa;intsb;scanf("%d", &t);while ( t-- ) {scanf("%d%d", &n, &m);memset(r + 1, TRUE, n * sizeof(char));for ( i = 1; i <= n; i++ )set[i] = i;while ( m-- ) {scanf("\n%c%d%d", &cmd, &a, &b);sa = find(a);sb = find(b);switch (cmd) {case 'A' :if ( sa != sb ) {puts("Not sure yet.");break;}if ( r[a] == r[b] ) {puts("In the same gang.");break;}puts("In different gangs.");break;default :if ( sa != sb ) {set[sa] = sb;r[sa] = !!( r[a] ^ r[b] );break;}}}}return 0;}
思路二:敌人的敌人就是朋友
注释代码:
/* * Problem ID : POJ 1703 Find them, Catch them * Author : Lirx.t.Una * Language : C++ * Run Time : 297 ms * Run Memory : 912 KB */ #include <stdio.h>//前一半表示罪犯所在帮派,后一半表示罪犯的对手所在的帮派#defineMAXN200001intset[MAXN];intfind(int x) {if ( x == set[x] )return x;return set[x] = find( set[x] );}voidunion_set( int sx, int y ) {intsy;sy = find(y);if ( sx == sy )return ;set[sx] = sy;}intmain() {intt;intn;intm;inti;inta, b;intsa, sb;charcmd;scanf("%d", &t);while ( t-- ) {scanf("%d%d", &n, &m);for ( i = 1; i <= n + n; i++ )set[i] = i;while ( m-- ) {scanf("\n%c%d%d", &cmd, &a, &b);sa = find(a);sb = find(b);switch (cmd) {case 'A' :if ( sa == sb ) {puts("In the same gang.");break;}if ( sa == find( b + n ) ) {//若a和b的对手在同一帮派 //则表示a、b肯定在不同帮派puts("In different gangs.");break;}puts("Not sure yet.");break;default :union_set( sa, b + n );//将a和b的对手合并到一个帮派union_set( sb, a + n );//同时也需要将b和a的对手合并到一个帮派break;}}}return 0;}无注释代码:
#include <stdio.h>#defineMAXN200001intset[MAXN];intfind(int x) {if ( x == set[x] )return x;return set[x] = find( set[x] );}voidunion_set( int sx, int y ) {intsy;sy = find(y);if ( sx == sy )return ;set[sx] = sy;}intmain() {intt;intn;intm;inti;inta, b;intsa, sb;charcmd;scanf("%d", &t);while ( t-- ) {scanf("%d%d", &n, &m);for ( i = 1; i <= n + n; i++ )set[i] = i;while ( m-- ) {scanf("\n%c%d%d", &cmd, &a, &b);sa = find(a);sb = find(b);switch (cmd) {case 'A' :if ( sa == sb ) {puts("In the same gang.");break;}if ( sa == find( b + n ) ) {puts("In different gangs.");break;}puts("Not sure yet.");break;default :union_set( sa, b + n );union_set( sb, a + n );break;}}}return 0;}
单词解释:
chaos:n, 混乱
launch:vt, 发射
launch action:采取行动,启动任务
root up:vt, 根除,肃清
gangs:n, 帮派,黑帮
criminal:n, 罪犯
clan:n, 集团,部落
incomplete:adj, 不完全的,不完备的
gangster:n, 歹徒
0 0
- POJ 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- POJ-1703 Find them, Catch them
- Poj 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- POJ 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- POJ 1703 - Find them, Catch them
- POJ 1703 Find them, Catch them
- POJ 1703(Find them, Catch them)
- POJ 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- poj 1703 Find them, Catch them
- POJ 1703: Find them, Catch them
- poj 1703 find them,catch them
- POJ--1703--Find them, Catch them
- POJ 1703 Find them, Catch them
- 新兵报到
- qt中qDebug()无法输出解决办法
- vim格式化代码
- Ubuntu和RedHat的使用
- SVN
- POJ 1703 Find them, Catch them
- 消息机制--AsyncTask(2)
- gvim实用配置
- 为什么应该写博客
- 字符编码ASCII、UNICODE与UTF8,UTF16的含义等
- Android中线程通讯类Handler
- 关于Loader,activity,Fragment之间的一个使用
- php 导出excel CVS格式
- 多协议多功能数据传输蓝牙解决方案