codeforces 129 B. Students and Shoelaces

http://codeforces.com/contest/129/problem/B 题意:n个点。m条边。每一次会将图中度为1的点加入到等待队列中。然后一起删掉,记为一次操作。当删掉一个点的时候,与它相连的边也全部删掉。问一共做进行多少次操作。使得图中不再有度为1的点。 思路:重点是用开一个数组deg[i]记录点i的度。这样比用.size()高明太多。。因为我们并不需要知道具体删了哪条边。我们只要知道与点i相连的点的边数因为点i被删除而减少了1.

/* ***********************************************
Author :111qqz
Created Time :2015年12月05日 星期六 10时55分46秒
File Name :code/cf/problem/129B.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=105;
 7int n,m;
 8vector<int>edge[N];
 9int deg[N];
10int main()
11{
12	#ifndef  ONLINE_JUDGE 
13	freopen("code/in.txt","r",stdin);
14  #endif
15	ms(deg,0);
16	scanf("%d %d",&n,&m);
17	for ( int i = 0 ; i < m ; i++)
18	{
19	    int u,v;
20	    scanf("%d %d",&u,&v);
21	    edge[u].push_back(v);
22	    edge[v].push_back(u);
23	    deg[u]++;
24	    deg[v]++;  //记录下度..比用.size()然后再去找边高明的多。。因为并不需要知道到底删了哪条边。
25	}
 1	bool flag;
 2	int ans = 0 ;
 3	while (1)
 4	{
 5	    flag = false;
 6	    int tmp[N];
 7	    memcpy(tmp,deg,sizeof(deg));  //因为是先训斥,把当前所有的训斥完再一起T出去。
 8	    for ( int i = 1 ; i <= n ; i++)
 9	    {
10		if (deg[i]==1)
11		{
12		    flag = true;
13		    tmp[i]--;
14		    for ( int j = 0 ; j < edge[i].size(); j++)
15		    {
16			int to = edge[i][j];
17			tmp[to]--; //不需要知道断了和谁的联系,只需要知道连接的数目少了1就好了。
18		    }
1		}
2	    }
3	    if (!flag) break;
4	    memcpy(deg,tmp,sizeof(tmp));
5	    ans++;
6	  //  puts("zzzzz");
7	}
8	printf("%d\n",ans);
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}