Lausanne Capitale Olympique
64-bit integer IO format: %lld Java class name: Main
Lausanne is a city in the French-speaking part of Switzerland. The headquarters of the International Olympic Committee (IOC) are located here; therefore Lausanne is also called the Olympic Capital.
As the London 2012 Olympics is coming, the IOC members frequently come to Lausanne to have meetings. Fortunately, IOC has a partner Lausanne Airways, which provides the IOC members with free flights.
Nevertheless, the IOC members live all over the world, and flights of Lausanne Airways are limited, so sometimes several transfers are unavoidable. For example, if a member lives in Shanghai, maybe he needs first fly to Beijing, then Paris, then Geneva, and finally arrived in Lausanne, which contains 3 transfers.
For convenience’s sake, the IOC members will always choose the trip with the minimum transfer times. However, due to the bad weather, some flights may be canceled, thus some members’ trip must be rescheduled. Lausanne Airways predicts that if there is only one flight canceled, then the minimum transfer times increases at most one for any IOC member.
Please check whether the above prediction is accomplished. Please note that all the flights mentioned above are two-way.
Input
Then there follows m lines, each of them contains two integers x, y (1 ≤ x, y ≤ n, x ≠ y), indicates there is a flight between city x and city y. It is guaranteed that there is at most one flight between any two cities, and these n cities are connected.
Output
If after a certain flight being canceled, these n cities are no longer connected, also output 0.
Sample Input
5 6 1 2 1 3 1 4 3 4 2 5 3 5
Sample Output
0
解题:。。哈哈,假设去掉边u->v,那么1到v就要改道,长度不能大于1啊,如果能够到v的变长不超过1,那么经过v的其他道路最长也不会增加超过1.
所以,看v的邻边,如果可以从v的邻点到此,那么就好了,要满足增长不大于1,那么1到v的邻点的长度应该为d[v]或者为d[v]-1。。。好了,直接枚举点的邻点就好了。。。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 10010; 18 struct arc{ 19 int to,next; 20 arc(int x = 0,int y = -1){ 21 to = x; 22 next = y; 23 } 24 }; 25 arc e[maxn*20]; 26 int head[maxn],d[maxn],q[maxn*10]; 27 int tot,n,m; 28 bool done[maxn]; 29 void add(int u,int v){ 30 e[tot] = arc(v,head[u]); 31 head[u] = tot++; 32 e[tot] = arc(u,head[v]); 33 head[v] = tot++; 34 } 35 void bfs(){ 36 int hd = 0,tl = 0; 37 for(int i = 1; i <= n; ++i) d[i] = INF; 38 d[1] = 0; 39 q[tl++] = 1; 40 while(hd < tl){ 41 int u = q[hd++]; 42 for(int i = head[u]; ~i; i = e[i].next){ 43 if(d[e[i].to] > d[u] + 1){ 44 d[e[i].to] = d[u] + 1; 45 q[tl++] = e[i].to; 46 } 47 } 48 } 49 } 50 int main() { 51 int u,v; 52 while(~scanf("%d %d",&n,&m)){ 53 memset(head,-1,sizeof(head)); 54 for(int i = tot = 0; i < m; ++i){ 55 scanf("%d %d",&u,&v); 56 add(u,v); 57 } 58 bfs(); 59 bool ans = true; 60 for(int i = 2; ans && i <= n; ++i){ 61 int cnt = 0; 62 for(int k = head[i]; ~k; k = e[k].next) 63 if(d[e[k].to] <= d[i] && ++cnt > 2) break; 64 if(cnt < 2) ans = false; 65 } 66 printf("%d\n",ans); 67 } 68 return 0; 69 }