Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

最大1000都过了。 数据不是一般的弱啊

Posted by hehexiaobai at 2011-03-23 19:18:30 on Problem 3177
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int MAX = 1005;
inline int min(int a, int b) { return a > b ? b : a;  }
int low[MAX], dfn[MAX];
int mp[MAX][MAX];
int parent[MAX];
int belong[MAX], nedge = 0;   //belong[i] 的值代表 节点i 属于 的边联通分量的序号   nedge代表边联通分量的个数
int n, r, time = 0;
int degree[MAX];

void tarjan(int u)
{
	time++;
	dfn[u] = low[u] = time;
	for(int i =1; i <= n; i ++)
		if(mp[u][i] == 1)
		{
				if(low[i] == 0)    //i  is not visited
				{
						parent[i] = u;
						tarjan(i);
						low[u] = min(low[u], low[i]); 
						if(dfn[u] < low[i])//(u, i)是桥, 值为2的边标记为桥
							mp[u][i] ++;
				}
				else
				{
						if(parent[u] != i)
								low[u] = min(low[u], dfn[i]);
				}
		}
}
//去掉是桥的那些边, 进行dfs, 找到所有的边联通分量
void  dfs(int u)   //形成一棵新的树
{
	belong[u] = nedge;
	for(int i = 1; i <= n; i ++)
		if(  parent[i] == u && mp[u][i] ==1 && belong[i] == 0)
		{
			dfs(i);
		}
}



int main()
{
	memset(low, 0, sizeof (low));
	memset(dfn, 0, sizeof (dfn));
	memset(mp, 0, sizeof(mp));
	memset(belong, 0, sizeof (belong));
	memset(degree, 0, sizeof (degree));

	scanf("%d %d",&n, &r);
	int s, t;
	for(int i = 1 ; i <= r; i ++)
	{
		scanf("%d %d",&s, &t);
		mp[s][t] = 1;
		mp[t][s] = 1;
	}

	tarjan(1);
	//for(int i =1 ; i <= n; i ++)
	//{
		//cout << dfn[i] <<" "<< low[i]<<"  "<< parent[i] << endl;
	//}
	//for(int i = 1; i <= n; i ++,cout << endl)
		//for(int j = 1; j <= n; j ++)
	//		cout << mp[i][j] <<' ';
//	cout << endl;

	nedge = 0;
	for(int i =  1;  i <= n; i ++)
		if(belong[i] == 0)  //如果i节点所属于哪个边联通分量还没确定,则开始搜索
		{
			nedge ++;
			dfs(i);
		}

	//for(int j = 1; j <= n; j++)
		//	cout << belong[j] << ' ';
//	cout << endl;
	//统计入度为1的点
	for(int i =  1; i <= n; i ++)
		for(int j = 1; j <= n; j ++)	
		{
			if(mp[i][j] == 2)
			{
				degree[belong[i]]++;
				degree[belong[j]]++;
			}
		}

	int ans = 0;
	for(int i = 1 ;i <= nedge; i ++)
		if(degree[i] == 1)
			ans ++;

	printf("%d\n", (ans + 1)/2);

	cin >> ans;
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator