Description
Farmer John's farm suffered an earthquake. some cowshed were damaged, but fortunately, all the routes between the cowshed were still usable. FJ farm has P (1 <= P <= 30,000) cowshed, no. 1 .. p. article C (1 <= C <= 100,000) two-way roads are connected to these cowshed, numbered 1 .. c. the road uses I to connect A_ I and B _ I (1 <= a_ I <= P; 1 <= B _ I <= P ). the road Sutra may connect a_ I to itself, and there may be multiple roads between the two cowshed. the farm is in a cowshed numbered 1. the cows with N (1 <= n <= P) headers in different cowshed sent a text message report_j (2 <= report_j <= p) to indicate that their Cowshed (report_j) is not damaged, however, they cannot return to the farm through the road and without damaged cowshed. when FJ receives all the text messages, find the minimum number of Cowshed that cannot be returned to the farm. this number includes the damaged cowshed. note: the first 50 submissions will provide running results on some test data. input
* Row 3: numbers separated by three spaces: P, C, and N
* 2nd .. c + 1 row: number of spaces separated by two spaces in each row: A_ I and B _ I * Row C + 2 .. c + n + 1: number of rows per row: report_j output
* Row 1st: number of cows that cannot return to the farm at least (including the damaged cowshed ).
Question:
For a report_j, we can only know that J and J are not connected to the farm.
So mark these points. When BFS does not access these points, count them.
Code:
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>//by zrt//problem:using namespace std;int H[30005],X[210000],P[210000],tot;int p,c,n;inline void add(int x,int y){ P[++tot]=y;X[tot]=H[x];H[x]=tot;}bool mark[30005];bool vis[30005];queue<int> q;int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif scanf("%d%d%d",&p,&c,&n); for(int i=0,x,y;i<c;i++){ scanf("%d%d",&x,&y); add(x,y);add(y,x); } for(int i=0,x;i<n;i++){ scanf("%d",&x); for(int j=H[x];j;j=X[j]){ mark[P[j]]=1; } } q.push(1); while(!q.empty()){ int x=q.front();q.pop(); vis[x]=1; for(int i=H[x];i;i=X[i]){ if(!mark[P[i]]&&!vis[P[i]]){ q.push(P[i]); } } } int ans=0; for(int i=2;i<=p;i++){ if(!vis[i]) ans++; } printf("%d\n",ans); return 0;}
Bzoj 1574: [usaco 2009 Jan] Earthquake Damage damage