codeforces 129 B. Students and Shoelaces
http://codeforces.com/contest/129/problem/B 题意:n个点。m条边。每一次会将图中度为1的点加入到等待队列中。然后一起删掉,记为一次操作。当删掉一个点的时候,与它相连的边也全部删掉。问一共做进行多少次操作。使得图中不再有度为1的点。 思路:重点是用开一个数组deg[i]记录点i的度。这样比用.size()高明太多。。因为我们并不需要知道具体删了哪条边。我们只要知道与点i相连的点的边数因为点i被删除而减少了1.
1/* ***********************************************
2Author :111qqz
3Created Time :2015年12月05日 星期六 10时55分46秒
4File Name :code/cf/problem/129B.cpp
5************************************************ */
6
7#include <cstdio>
8#include <cstring>
9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25
26
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=105;
34int n,m;
35vector<int>edge[N];
36int deg[N];
37int main()
38{
39 #ifndef ONLINE_JUDGE
40 freopen("code/in.txt","r",stdin);
41 #endif
42 ms(deg,0);
43 scanf("%d %d",&n,&m);
44 for ( int i = 0 ; i < m ; i++)
45 {
46 int u,v;
47 scanf("%d %d",&u,&v);
48 edge[u].push_back(v);
49 edge[v].push_back(u);
50 deg[u]++;
51 deg[v]++; //记录下度..比用.size()然后再去找边高明的多。。因为并不需要知道到底删了哪条边。
52 }
53
54 bool flag;
55 int ans = 0 ;
56 while (1)
57 {
58 flag = false;
59 int tmp[N];
60 memcpy(tmp,deg,sizeof(deg)); //因为是先训斥,把当前所有的训斥完再一起T出去。
61 for ( int i = 1 ; i <= n ; i++)
62 {
63 if (deg[i]==1)
64 {
65 flag = true;
66 tmp[i]--;
67 for ( int j = 0 ; j < edge[i].size(); j++)
68 {
69 int to = edge[i][j];
70 tmp[to]--; //不需要知道断了和谁的联系,只需要知道连接的数目少了1就好了。
71 }
72
73 }
74 }
75 if (!flag) break;
76 memcpy(deg,tmp,sizeof(tmp));
77 ans++;
78 // puts("zzzzz");
79 }
80 printf("%d\n",ans);
81
82 #ifndef ONLINE_JUDGE
83 fclose(stdin);
84 #endif
85 return 0;
86}