#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include//assert(); //#include //xAOJ ///////// #define REP(i, x, n) for(int i = x; i < n; i++) #define rep(i,n) REP(i,0,n) #define P(p) cout<<(p)< ///////// typedef long long LL; typedef long double LD; typedef unsigned long long ULL; ///////// using namespace::std; ///////// ///////// class UnionFind{ public: int cNum;//要素数 vector parent; vector color; vector< set< int > > tonari; UnionFind(int n){ cNum = n; parent = vector(n); color = vector(n); tonari = vector< set >(n); for(int i=0;i::iterator it,endit; it = tonari[y].begin(); endit = tonari[y].end(); for( ;it != endit;++it ){ tonari[x].insert( *it ); } } void tonariAdd(int atti,int kotti){ tonari[ find(atti) ].insert(kotti); tonari[ find(kotti) ].insert(atti); } }; void solve(){ int H,W; cin >> H >> W; UnionFind UF(H*W); for(int h=0;h> UF.color[h*W + w]; } } //////////////// if( 1> Q; int R,C,X; set::iterator it,endit; int ter; set tempset; while(Q--){ cin >> R >> C >> X; --R;--C; ter = UF.find(R*W+C); if( UF.color[ ter ] == X ) continue; UF.color[ter] = X; tempset = UF.tonari[ ter ]; it = tempset.begin(); endit = tempset.end(); for(;it != endit;++it){ UF.add( ter, *it); } } for(int h=0;h