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不知道多少次了,在ZOJ上过,在这里就是不过,求大神帮忙看看

Posted by kimi940211 at 2014-05-21 00:37:16 on Problem 1470
#include<iostream>
#include<stdio.h>
#include<string>
#include<memory.h>
#include<algorithm>
#include<vector>
using namespace std;

int n;
vector<int> node[5000];
int parent[5000];
int deep[5000];
long coun[5000];
int answer[1001][1001];

void DFS(int dep, int x)
{
	deep[x] = dep;
	for (vector<int>::iterator it = node[x].begin(); it != node[x].end(); it++)
	{
		int next = *it;
		DFS(dep + 1, next);
	}
}

int LCA(int x, int y)
{
	if (answer[x][y] > 0)return answer[x][y];
	if(x==y)
	{
		answer[x][y]=x;
		answer[y][x]=answer[x][y];
		return answer[x][y];
	}
	if (deep[x] < deep[y])
	{
		if (parent[y] == x)
		{
			answer[x][y] = x;
			answer[y][x]=answer[x][y];
			return x;
		}
		else
		{
			answer[x][y] = LCA(x, parent[y]);
			answer[y][x]=answer[x][y];
			return answer[x][y];
		}
	}
	else if (deep[x]>deep[y])
	{
		if (parent[x] == y)
		{
			answer[x][y] = y;
			answer[y][x]=answer[x][y];
			return y;
		}
		else
		{
			answer[x][y] = LCA(parent[x], y);
			answer[y][x]=answer[x][y];
			return answer[x][y];
		}
	}
	else
	{
		answer[x][y] = LCA(x, parent[y]);
		answer[y][x]=answer[x][y];
		return answer[x][y];
	}
}

int main()
{
	while(scanf("%d",&n))
	{
		memset(parent, 0, sizeof(parent));
		for (int iiii = 1; iiii <= n; iiii++)
		{
			node[iiii].clear();
		}
		for (int iiiii = 1; iiiii <= n; iiiii++)
		{
			int tempnode, num, successor;
			scanf("%d:(%d)", &tempnode, &num);
			for (int j = 1; j <= num; j++)
			{
				scanf("%d",&successor);
				node[tempnode].push_back(successor);
				parent[successor] = tempnode;
			}
		}
		memset(answer, 0, sizeof(answer));
		int root;
		for (int i = 1; i <= n; i++)
		{
			if (parent[i] == 0)
			{
				root = i;
				break;
			}
		}
		DFS(0, root);
		memset(coun, 0, sizeof(coun[0])*(n + 1));
		int m;
		scanf("%d",&m);
		for (int iii = 1; iii <= m; iii++)
		{
			int x, y;
			while (getchar() != '(');
			scanf("%d%d", &x, &y);
			long res = LCA(x, y);
			coun[res]++;
		}
		for (int ii = 1; ii <= n; ii++)
		{
			if (coun[ii] > 0)
			{
				cout << ii << ":" << coun[ii] << endl;
			}
		}
	}
	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