BAPC 2016 ----Brexit (BFS + vector)

听说这道题和vector更配哦~

用vector存放盟友,这道题就非常简单了

题意:

一个由C个国家组成的联盟,有一天L离开了,对于任意国家,只要盟友少于之前的一半,他也会离开,问X会不会离开

思路:

用vector记录与当前国家相连的国家,num数组记录开始各个国家的盟友数目,lost记录离开联盟的盟友个数,

BFS把离开的国家跑一遍答案就出来了

 1 # include <iostream>
 2 # include <stdio.h>
 3 # include <string.h>
 4 # include <memory>
 5 # include <cmath>
 6 # include <string>
 7 # include <cstdlib>
 8 # include <queue>
 9 # include <vector>
10 # include <algorithm>
11 # include <set>
12 # include <map>
13 
14 using namespace std;
15 
16 int num[1000005], lost[1000005], vis[1000005];
17 queue <int> q;
18 vector <int> G[1000005];
19 
20 int main()
21 {
22     int C, P, X, L;
23     int x, y, v, i, flag, u;
24 
25     memset(num, 0, sizeof(num));
26     memset(lost, 0, sizeof(lost));
27     memset(vis, 0, sizeof(vis));
28 
29     scanf("%d %d %d %d", &C, &P, &X, &L);
30     while(P--)
31     {
32         scanf("%d %d", &x, &y);
33         G[x].push_back(y);
34         G[y].push_back(x);
35         num[x]++;
36         num[y]++;
37     }
38 
39     q.push(L);
40     flag = 1;
41     vis[L] = 1;  //开始没标记L,wa了一下午
42     while(!q.empty())
43     {
44         u = q.front();
45         q.pop();
46         if(u==X)
47         {
48             flag = 0;
49             printf("leave
");
50             break;
51         }
52         else
53         {
54             int NN = G[u].size();
55             for(i=0; i<NN; i++)
56             {
57                 v = G[u][i];
58                 lost[v]++;
59                 if(lost[v]*2>=num[v]&&vis[v]==0)
60                 {
61                     q.push(v);
62                     vis[v] = 1;
63                 }
64             }
65         }
66 
67     }
68     if(flag==1) printf("stay
");
69     return 0;
70 }
原文地址:https://www.cnblogs.com/0xiaoyu/p/11383215.html