Idea: Naked and check set template AH.
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstdlib>5#include <cstring>6#include <map>7#include <queue>8#include <deque>9#include <cmath>Ten#include <vector> One#include <ctime> A#include <cctype> -#include <Set> - the using namespacestd; - - #defineMem0 (a) memset (a, 0, sizeof (a)) - #defineLson L, M, RT << 1 + #defineRson m + 1, R, RT << 1 | 1 - #definedefine_m int m = (L + r) >> 1 + #defineRep (A, b) for (int a = 0; a < b; a++) A #defineLowbit (x) ((x) & (-(x))) at #defineCONSTRUCTINT4 (name, a, B, C, D) name (int a = 0, int b = 0, int c = 0, int d = 0): A (a), B (b), C (c), D (d) {} - #defineCONSTRUCTINT3 (name, a, B, c) name (int a = 0, int b = 0, int c = 0): A (a), B (b), C (c) {} - #defineConstructInt2 (name, a, b) name (int a = 0, int b = 0): A (a), B (b) {} - -typedefDoubledb; -typedefLong LongLL; intypedef pair<int,int>PII; -typedef multiset<int>MSI; totypedef multiset<int>:: iterator msii; +typedefSet<int>si; -typedefSet<int>:: iterator sii; thetypedef vector<int>VI; * $ Const intdx[8] = {1,0, -1,0,1,1, -1, -1};Panax Notoginseng Const intdy[8] = {0, -1,0,1, -1,1,1, -1}; - Const intMAXN = 1e5 +7; the Const intMAXM = 1e5 +7; + Const intMAXV = 1e7 +7; A Const intMD = 1e9 +7; the Const intINF = 1e9 +7; + Const DoublePI = ACOs (-1.0); - Const DoubleEPS = 1e-Ten; $ $ structUnionfindsets { - vi F; - intN; the unionfindsets () {f.clear ();} - voidClear () {f.clear ();}Wuyi voidResizeintN) {n = n +2; F.resize (n +5); } the voidInit () { for(inti =1; I <= N; i++) F[i] =i;} - int Get(intu) {if(U = = F[u])returnUreturnF[u] =Get(F[u]);} Wu voidAddintUintV) {f[Get(u)] =Get(v);} - BOOLFindintUintV) {return Get(u) = =Get(v);} About }; $ - unionfindsets UFs; - - BOOLmark[100007]; A + intMain () { the //freopen ("In.txt", "R", stdin); -Ufs.resize (1e5 +7); $ UFS. Init (); the intv =0, A, b, yes =1; the while(1) { thescanf"%d%d", &a, &b); the if(!v) v =A; -Mark[a] = mark[b] =1; in if(A = =-1&& b = =-1) Break; the if(A = =0&& b = =0) { the for(inti =1; I <= 1e5; i++) { About if(Mark[i] && ufs.Get(i)! = UFs.Get(v)) { theYes =0; the Break; the } + } - if(yes) puts ("Yes"); the ElsePuts"No");Bayi UFS. Init (); the Mem0 (Mark); thev =0; -Yes =1; - Continue; the } the if(Ufs.find (A, B)) { theYes =0; the } - Ufs.add (A, b); the } the return 0; the}
View Code
[hdu1272] and check set