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 cuiaoxiang at 2006-02-19 21:52:57 on Problem 2568
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

const int N = 200;
vector<int> tr[N];
int deg[N], a[N], b[N];

void Print(int u)
{
	flag[u] = true;
	cout << "(" << u;
	for (vector<int>::iterator it = tr[u].begin(); it != tr[u].end(); ++it)
	{
		cout << " ";
		Print(*it);
	}
	cout << ")";
}

int main()
{
	string buf;
	int n, i, m, x;
	while (getline(cin, buf))
	{
		stringstream ss(buf);
		for (n = 0; ss >> a[n]; n++);
		n = n + 1;
		memset(deg, 0, sizeof(deg));
		for (i = 0; i < n - 1; i++)
			deg[a[i]]++;
		m = 0;
		for (i = 1; i <= n; i++)
			if (deg[i] == 0)
				b[m++] = i;
		make_heap(b, b + m, greater<int>());
		for (i = 1; i <= n; i++)
			tr[i].clear();
		for (i = 0; i < n - 1; i++)
		{
			x = b[0];
			b[0] = b[--m];
			pop_heap(b, b + m, greater<int>());
			tr[a[i]].push_back(x);
			if (--deg[a[i]] == 0)
			{
				b[m++] = a[i];
				push_heap(b, b + m, greater<int>());
			}
		}
		Print(n);
		cout << 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