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

无限wa,不明白错哪了

Posted by hataksumo at 2012-08-24 11:17:07 on Problem 3352
#include<cstdio>
#include<cstring>
#define MN 1100
#define ME 2200
struct  
{
	int to;
	int pre;
}edges[ME];
int box[MN],len;
void iniPreLinkList(int n)
{
	memset(box,-1,sizeof(box));
	len = 0;
}
void addDirectedEdge(int frm,int to)
{
	edges[len].to = to;
	edges[len].pre = box[frm];
	box[frm] = len++;
}
void addDoubleEdge(int a,int b)
{
	addDirectedEdge(a,b);
	addDirectedEdge(b,a);
}
int father[MN],order[MN];
bool visited[ME];

int find(int x)
{
	if(x!=father[x])
		father[x]=find(father[x]);
	return father[x];
}


void tardfs(int cur)
{
	int ads,ct;
	for(ads = box[cur];~ads;ads=edges[ads].pre)
	{
		if(visited[ads])continue;
		ct = edges[ads].to;
		if(order[ct]==0)
		{
			visited[ads] = true;
			visited[ads^1] = true;
			order[ct] = order[cur]+1;
			tardfs(ct);
		}
		if(order[find(ct)]<order[find(cur)])
				father[find(cur)] = find(ct);
	}
}

int tarjan(int n)
{
	int i;
	int isolated_point=0;
	for(i=1;i<=n;i++)
	{
		father[i] = i;
		order[i] = 0;
	}
	memset(visited,false,sizeof(visited));
	for(i=1;i<=n;i++)
	{
		if(!order[i])
		{
			isolated_point++;
			order[i] = 1;
			tardfs(i);
		}
	}
	if(isolated_point==1)
		isolated_point=0;
	return isolated_point;
}

//这里order做判重用
//题中给的是连通的图,所以不需要考虑不连通的情况
int solve(int n)
{
	int ans=0,cnt,i,rt,ads,ct;
	for(i=1;i<=n;i++)
	{
		rt = find(i);
		if(order[rt])
		{
			cnt=0;
			order[rt]=0;
			for(ads=box[rt];~ads;ads=edges[ads].pre)
			{
				ct = edges[ads].to;
				if(find(ct)!=rt)
					cnt++;
			}
			if(cnt==1)
				ans++;
		}
	}
	return (ans+1)/2;
}

int main()
{
	char sample[100];
	int n,m,a,b,i;
	int isolated_point;//考虑一下不连通,加不加都是wa啊
	while(gets(sample))
	{
		if(strlen(sample)<3)continue;
		scanf("%d%d",&n,&m);
		iniPreLinkList(n);
		for(i=0;i<m;i++)
		{
			scanf("%d%d",&a,&b);
			addDoubleEdge(a,b);
		}
		isolated_point=tarjan(n);
		printf("Output for %s\n",sample);
		printf("%d\n",solve(n)+isolated_point);
	}
	return 0;
}
/*
Sample Input 1
10 12
1 2
1 3
1 4
2 5
2 6
5 6
3 7
3 8
7 8
4 9
4 10
9 10

Sample Input 2
3 3
1 2
2 3
1 3

我在下一世等你
11 14
1 2
1 3
1 4
2 5
6 11
2 6
5 6
5 11
3 7
3 8
7 8
4 9
4 10
9 10

看那温暖晨曦
11 14
1 2
1 3
1 4
5 11
2 5
2 6
5 6
6 11
3 7
3 8
7 8
4 9
4 10
9 10

美丽的程序娘小優YoU
5 4
1 2
2 4
4 3
2 5

幽幽子
3 3
1 2
2 3
3 1
*/

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