#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 temp; set::iterator it,endit; it = tonari[x].begin(); endit = tonari[x].end(); for( ;it != endit;++it ){ //tonari[x].insert( *it ); if( !same(y,*it) ){ temp.insert( *it ); } } it = tonari[y].begin(); endit = tonari[y].end(); for(; it != endit;++it){ if( !same(y,*it) ){ temp.insert( *it ); } } tonari[y] = temp; } 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 0 ){ if( UF.color[h*W+w] == UF.color[(h-1)*W + w] ){ UF.add(h*W+w,(h-1)*W+w ); }else{ UF.tonariAdd(h*W+w,(h-1)*W+w); } } if( w> 0 ){ if( UF.color[h*W+w] == UF.color[h*W+(w-1)] ){ UF.add( h*W+w,h*W+(w-1) ); }else{ UF.tonariAdd(h*W+w,h*W+(w-1) ); } } } } //////////////// int Q; cin >> 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); } /* cout << "//\n"; for(int h=0;h