51nod 1299 监狱逃离

1299 监狱逃离
基准时间限制:1 秒 空间限制:131072 KB
监狱有N条道路连接N + 1个交点,编号0至N,整个监狱被这些道路连在一起(任何2点之间都有道路),人们通过道路在交点之间走来走去。其中的一些交点只有一条路连接,这些点是监狱的出口。在各个交点中有M个点住着犯人(M <= N + 1),剩下的点可以安排警卫,有警卫把守的地方犯人无法通过。给出整个监狱的道路情况,以及犯人所在的位置,问至少需要安排多少个警卫,才能保证没有1个犯人能够逃到出口,如果总有犯人能够逃出去,输出-1。
 
 
如上图所示,点1,6 住着犯人,0,4,5,7,8是出口,至少需要安排4个警卫。
Input
第1行:2个数N, M中间用空格分隔,N表示道路的数量,M表示犯人的数量(1<= N <= 100000, 0 <= M <= N + 1)。
之后N行:每行2个数S, E中间用空格分隔,表示点编号为S的点同编号为E的点之间有道路相连。(0 <= S, E <= N)。
之后的M行,每行1个数Pi,表示编号为Pi的点上有犯人。
Output
输出1个数对应最少需要多少警卫才能不让犯人逃出监狱。
Input示例
8 2
0 1
1 2
2 3
3 4
3 5
2 6
6 8
6 7
1
6
Output示例
4
仔细推就行了。
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<cstdlib>
 7 #include<vector>
 8 using namespace std;
 9 typedef long long ll;
10 typedef long double ld;
11 typedef pair<int,int> pr;
12 const double pi=acos(-1);
13 #define rep(i,a,n) for(int i=a;i<=n;i++)
14 #define per(i,n,a) for(int i=n;i>=a;i--)
15 #define Rep(i,u) for(int i=head[u];i;i=Next[i])
16 #define clr(a) memset(a,0,sizeof(a))
17 #define pb push_back
18 #define mp make_pair
19 #define fi first
20 #define sc second
21 #define pq priority_queue
22 #define pqb priority_queue <int, vector<int>, less<int> >
23 #define pqs priority_queue <int, vector<int>, greater<int> >
24 #define vec vector
25 ld eps=1e-9;
26 ll pp=1000000007;
27 ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
28 ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
29 void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
30 //void add(int x,int y,int z){ v[++e]=y; next[e]=head[x]; head[x]=e; cost[e]=z; }
31 int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
32 ll read(){ ll ans=0; char last=' ',ch=getchar();
33 while(ch<'0' || ch>'9')last=ch,ch=getchar();
34 while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
35 if(last=='-')ans=-ans; return ans;
36 }
37 bool flag,fl[100005];
38 int ans,v[200005],Next[200005],head[200005],e,in[200005],root,dp[200005];
39 void add(int x,int y){ v[++e]=y; Next[e]=head[x]; head[x]=e;}
40 void dfs(int x,int f){
41     if (flag) return ;
42     int ans1=0,ans2=0,ans3=0;
43     for (int i=head[x];i;i=Next[i])
44     if (v[i]!=f){
45         dfs(v[i],x);
46         if (dp[v[i]]==1) ans1++;
47         if (dp[v[i]]==2) ans2++;
48         if (dp[v[i]]==3) ans3++;
49     }
50     if (fl[x]){
51         if (!ans1 && !ans2 && !ans3) {flag=1; return;}
52         ans+=ans1; dp[x]=2;
53     } else {
54         if (ans1 && ans2){
55             ans++;
56             dp[x]=3;
57         }
58         if (ans1 && !ans2) {dp[x]=1;}
59         if (ans2 && !ans1) {dp[x]=2;}
60         if (!ans1 && !ans2){
61             if (!ans3) {dp[x]=1;}
62             else {dp[x]=3;}
63         }
64     }
65 }
66 int main(){
67     int n=read(),m=read();
68     for (int i=1;i<=n;i++) {
69         int a=read(),b=read();
70         add(a,b); add(b,a);
71         in[a]++; in[b]++;
72         if (in[a]>=2) root=a;
73         if (in[b]>=2) root=b;
74     }
75     if (m==0){puts("0"); return 0;}
76     for (int i=1;i<=m;i++){
77         int a=read(); fl[a]=1;
78     }
79     dfs(root,-1);
80     if (flag)  puts("-1");
81     printf("%d",ans);
82     return 0;
83 }
View Code

 

posted @ 2017-10-07 21:12  SXia  阅读(299)  评论(1编辑  收藏  举报