并查集 POJ 1703
来源:互联网 发布:ubuntu如何安装输入法 编辑:程序博客网 时间:2024/05/17 08:25
题意:
有两个帮派,给你A信息,让你输出这两人是不是一个帮派的;给你D信息,表示这俩人不是一个帮派的,给你A信息后,每次输出你的回答
题解:
又是简单的并查集,但是很坑的就是cin会超时,然后并不知道是不是cout和scanf这种出现了不可预知到错误,然后就是init少了一个,唉,这题还是应该注意编号啊,以后就直接按编号来,别没事都从0开始
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<queue>#define pr(x) cout<<#x<<" "<<x;#define pl(x) cout<<#x<<" "<<x<<endl;#include<math.h>#include<algorithm>using namespace std;#define MAX_LEN 200010int fa[MAX_LEN];int ranks[MAX_LEN];void init(int n){ for(int i = 0;i<n;i++){ fa[i] = i; ranks[i] = 0; }}int find(int i){ if(i == fa[i]){return i;} else{ return fa[i] = find(fa[i]); //return fa[i]; }}void uint(int a, int b){ int x = find(a); int y = find(b); if(x == y){return ;} else{ if(ranks[x] > ranks[y]){ fa[y] = x; } else{ fa[x] = y; if(ranks[x]==ranks[y]){ ranks[y]++; } } }}bool same(int i,int j){ return find(i)==find(j);}int main(){ int t; scanf("%d",&t); while(t--){ int n,m; scanf("%d%d",&n,&m); init(2*n+2); //getchar(); char ch;int a,b; while(m--){ getchar(); scanf("%c%d%d",&ch,&a,&b); //getchar(); if(ch =='A'){ if(same(a,b)){ puts("In the same gang."); // cout << "In the same gang." << endl;//continue; } else if(same(a,b+n)){ puts("In different gangs."); // cout << "In different gangs." << endl; //continue; } else{ puts("Not sure yet."); // cout<<"Not sure yet."<<endl; // continue; } } else { uint(a,b+n); uint(a+n,b); } } } return 0;}
0 0
- POJ 1703 并查集
- poj-1703 并查集
- poj 1703 并查集
- POJ 1703 (并查集)
- poj 1703(并查集)
- POJ-1703 并查集
- POJ-1703 并查集
- POJ 1703 (并查集)
- poj 1703 并查集
- poj 1703 并查集
- poj 1703(并查集)
- POJ 1703 并查集
- POJ 1703 并查集
- POJ 1703 并查集
- 并查集 POJ 1703
- POJ 1703 并查集
- Poj 并查集
- poj并查集
- Cocos Creator 访问节点和组件(摘自官方文档)
- TFS入门使用教程--新建团队项目
- 使用l临界区来实现单例模式
- c++在linux下读取文件遇到的问题
- yaffs2移植到内核linux-4.4.16的修改记录
- 并查集 POJ 1703
- C#抽象类和接口的区别分析
- 【Linux学习】epoll详解
- oracle 查找4000字节以下的记录
- URL参数中&和+被转成空格
- Django 分页
- github使用总结
- java中的static(包括类前面修饰的static、方法前面修饰的static、成员变量前面修饰的static)
- Cocos Creator 常用节点和组件接口(摘自官方文档)