Kattis - flippingcards 【并查集】
题意
给出 N 对 数字 然后 每次从一对中 取出一个数字 判断 能否有一种取出的方案 取出的每个数字 都是不同的
思路 
将每一对数字 连上一条边  然后 最后 判断每一个连通块里面 边的个数 是否 大于等于 点的个数  用并查集判断
AC代码
#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
const double PI  = 3.14159265358979323846264338327;
const double E   = exp(1);
const double eps = 1e-6;
const int INF  = 0x3f3f3f3f;
const int maxn = 1e5 + 5;
const int MOD  = 1e9 + 7;
int pre[maxn];
int find(int x)
{
    int r = x;
    while (pre[r] != r)
        r = pre[r];
    int j = x, i;
    while (j != r)
    {
        i = pre[j];
        pre[j] = r;
        j = i;
    }
    return r;
}
void join(int x, int y)
{
    int fx = find(x), fy = find(y);
    if (x != fy)
        pre[fx] = fy;
}   
int p[maxn], q[maxn];
struct Node
{
    map <int, int> M;
    int tot;
};
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        scanf("%d", &n);
        for (int i = 0; i < maxn; i++)
            pre[i] = i;
        for (int i = 0; i < n; i++)
        {
            scanf("%d%d", &p[i], &q[i]);
            join(p[i], q[i]);
        }
        map <int, Node> m;
        int flag = 1;
        for (int i = 0; i < n; i++)
        {
            int num = find(p[i]);
            m[num].M[q[i]] = 1;
            m[num].M[p[i]] = 1;
            m[num].tot ++;
            if (m[num].M.size() < m[num].tot)
            {
                flag = 0;
                break;
            }
        }
        if (flag)
            printf("possible\n");
        else
            printf("impossible\n");
    }
}
												
											Kattis - flippingcards 【并查集】的更多相关文章
- Kattis - Virtual Friends(并查集)
		
Virtual Friends These days, you can do all sorts of things online. For example, you can use various ...
 - BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
		
4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...
 - 关押罪犯 and 食物链(并查集)
		
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
 - 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
		
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
 - bzoj1854--并查集
		
这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...
 - [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)
		
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
 - [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)
		
Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...
 - 【BZOJ-3673&3674】可持久化并查集       可持久化线段树 + 并查集
		
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
 - Codeforces 731C Socks 并查集
		
题目:http://codeforces.com/contest/731/problem/C 思路:并查集处理出哪几堆袜子是同一颜色的,对于每堆袜子求出出现最多颜色的次数,用这堆袜子的数目减去该值即为 ...
 
随机推荐
- STL学习笔记(string)
			
动机 C++标准程序库中的string class使我们可以将string当做一个一般型别.我们可以像对待基本型别那样地复制.赋值和比较string, 再也不必但系内存是否足够.占用的内存实际长度等问 ...
 - MFC错误集锦
			
MFC中相关报错及其解决的方法: (1)0x00000005: 解决的方法:看是哪里的 数组越界: (2)0xCCCCCCCC:在类中声明指针,但没有赋初值之类的错误. 解决的方法:在类的构造函数中给 ...
 - Python内置函数之staticmethod()
			
staticmethod(function)返回函数的静态方法.一般来说,实例对象调用类方法不用传入参数,因为实例对象本身隐式的作为第一个参数传入了.而采用静态方法之后,实例对象在调用类方法时必须传入 ...
 - [译]GLUT教程 - 重整子窗体
			
Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Reshape Subwindows 重整函数的回调需要处理两 ...
 - Android Studio 使用笔记:记录使用Gradle配置AndroidAnnotations
			
系统:Mac Yosemit 10.10 JDK:1.6+ Android Studio:1.2 原来看到有人用AndroidAnnotations,十分羡慕.但是Gradle并不熟悉,现找到了正确的 ...
 - UVA 699 The Falling Leaves (二叉树水题)
			
本文纯属原创.转载请注明出处,谢谢. http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central re ...
 - PHP-Manual的学习----【语言参考】----【类型】-----【对象】
			
Object 对象1.对象初始化要创建一个新的对象 object ,使用 new 语句实例化一个类: class foo{ function do_foo(){ echo &quo ...
 - java.lang.IllegalStateException: ImageView no longer exists. You should not use this PhotoViewAttacher any more.
			
java.lang.IllegalStateException: ImageView no longer exists. You should not use this PhotoViewAttach ...
 - SpringMVC拦截器实现登录认证
			
项目结构如图: 需要的jar:有springMVC配置需要的jar和jstl需要的jar SpringMVC包的作用说明: aopalliance.jar:这个包是AOP联盟的API包,里面包含了针对 ...
 - Latent Activity Trajectory (LAT)
			
https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/funcZone_TKDE_Zheng.pdf Specific ...