Question link: https://www.nowcoder.com/acm/contest/106#question
Question CProfessional Manager
#include <bits/stdc++.h> using namespace std; int root[200005],siz[200005],last[200005]; int n,q,u,v; int get(int i) { if(root[i]!=i) return get(root[i]); return root[i]; } void ops1() { scanf("%d%d",&u,&v); u=get(last[u]),v=get(last[v]); if(u==v) return; root[u]=v; you [v] + = you [u]; } void ops2() { scanf("%d",&u); siz[get(last[u])]--; you [ ++ n] = 1 ; last[u]=root[n]=n; } void ops3() { scanf("%d",&u); printf("%d\n",siz[get(last[u])]); } void ops4() { scanf("%d%d",&u,&v); if(get(last[u])==get(last[v])) printf("YES\n"); else printf("NO\n"); } intmain () { int t; scanf("%d",&t); for(int j=1;j<=t;j++) { printf("Case #%d:\n",j); scanf("%d%d",&n,&q); for(int i=1;i<=n;i++) last[i]=i,root[i]=i,siz[i]=1; while(q--) { int m; scanf("%d",&m); switch(m) { case 1: ops1();break; case 2: ops2();break; case 3: ops3();break; case 4: ops4();break; } } } return 0; } /* /// Use last to record the first position of the current element /// To query the root of the current element, query the root of the last of the current element ///// When an element is to be removed from the collection ///// 改变其last root[last]=last siz[last]=1 /// This will not affect the process of other elements under it querying the root When combined 2 3,3 4,4 5 last 3 4 5 5 | | | | root 2-3-4-5 you 1 2 3 4 At this point, if you remove 3 last 3 6 5 5 | | | | root 2-3-4-5 you 1 1 3 3 */
L题 Fresh Air
#include <algorithm> #include < string .h> #include <cstring> #include <stdio.h> #include <queue> using namespace std; struct NODE{ int x,y;}a[ 100005 ]; // supercalifragilisticexpialidocious The area is surrounded by trees and cannot be reached from the outside // cnt is the size of the unreachable area The unreachable area is set to 0 int n,cnt,G[ 2005 ][ 2005 ],ans[ 100005 ]; int mov[ 4 ][ 2 ]={{ 0 , 1 },{ 1,0},{0,-1},{-1,0}}; bool bound(int x,int y) { return x<0||x>2000||y<0||y>2000; } void bfs(int x,int y) { /// In the bfs process, it will stop when it encounters 1 /// The 0 that can be reached from this point will become 2, and the 0 that cannot be reached will remain unchanged queue <NODE> q; q.push((NODE){x,y}); G[x][y]=2; cnt--; while(!q.empty()) { NODE tmp=q.front(); q.pop(); for(int i=0;i<4;i++) { int nowx=tmp.x+mov[i][0], nowy =tmp.y+mov[i][ 1 ]; if (bound(nowx,nowy)) continue ; // out of bounds if (G[nowx][nowy]) continue ; // can reach or have visited G[ nowx][nowy]= 2 ; cnt--; // The point is reachable 0 cnt--that is, remove the point q.push((NODE){nowx,nowy}); } } } bool check(int x,int y) { for(int i=0;i<4;i++) { // If there is a situation where the search is stopped due to the tree at this point int nowx=x+mov[i][ 0 ], nowy =y+mov[i][ 1 ]; if (bound(nowx,nowy)) continue ; // out of bounds if (G[nowx][nowy]== 2 ) return 1 ; /// There are The point that was changed to 2 when bfs } return 0 ; } int main() { while(~scanf("%d",&n)) { for(int i=1;i<=n;i++) { scanf("%d%d",&a[i].x,&a[i].y); a[i].x+=1000, a[i].y+=1000; G[a[i].x][a[i].y]=1; } cnt=2001*2001-n; bfs( 0 , 0 ); // First bfs once /// At this time res is equal to the number of unreachable 0s left after subtracting 1 and 2 for ( int i=n;i> 0 ;i--) / / traverse forward from the last point { ans[i] =cnt++ ; G[a[i].x][a[i].y] = 0 ; /// Delete the tree placed at this point if (check(a[i].x,a[i].y)) bfs(a[i].x,a[i].y); // Continue bfs from this point } for ( int i= 1 ;i<=n;i++ ) printf("%d\n",ans[i]); } return 0; }