[USACO09JAN]Earthquake Damage
作者:互联网
刚开始因为没看到只能走没有损坏的农场,磨叽了20多分钟……不管了,写题解吧。
首先如果一个点不能到达原点,那么和他相邻的点也不能到达原点,所以刚开始我们把不能走的点和他相邻的点都打上标记,然后跑dfs就行了。
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 #include<cstdlib> 7 #include<vector> 8 #include<queue> 9 #include<stack> 10 #include<cctype> 11 using namespace std; 12 #define enter puts("") 13 #define space putchar(' ') 14 #define Mem(a) memset(a, 0, sizeof(a)) 15 typedef long long ll; 16 typedef double db; 17 const int INF = 0x3f3f3f3f; 18 const db eps = 1e-8; 19 const int maxn = 3e4 + 5; 20 inline ll read() 21 { 22 ll ans = 0; 23 char ch = getchar(), last = ' '; 24 while(!isdigit(ch)) last = ch, ch = getchar(); 25 while(isdigit(ch)) ans = (ans << 3) + (ans << 1) + ch - '0', ch = getchar(); 26 if(last == '-') ans = -ans; 27 return ans; 28 } 29 inline void write(ll x) 30 { 31 if(x < 0) putchar('-'), x = -x; 32 if(x >= 10) write(x / 10); 33 putchar(x % 10 + '0'); 34 } 35 36 int p, c, n; 37 vector<int> v[maxn]; 38 bool vis[maxn]; 39 int ans; 40 41 void dfs(int now) 42 { 43 vis[now] = 1; ans--; 44 for(int i = 0; i < (int)v[now].size(); ++i) 45 if(!vis[v[now][i]]) dfs(v[now][i]); 46 } 47 48 int main() 49 { 50 p = read(); c = read(); n = read(); 51 for(int i = 1; i <= c; ++i) 52 { 53 int x = read(), y = read(); 54 v[x].push_back(y); v[y].push_back(x); 55 } 56 for(int i = 1; i <= n; ++i) 57 { 58 int x = read(); vis[x] = 1; 59 for(int j = 0; j < (int)v[x].size(); ++j) vis[v[x][j]] = 1; 60 } 61 ans = p; dfs(1); 62 write(ans); enter; 63 return 0; 64 }View Code
标签:now,ch,int,Damage,read,USACO09JAN,ans,include,Earthquake 来源: https://blog.51cto.com/u_15234622/2830904