Skip to main content
  1. Posts/

hdoj 1285 确定比赛名次

Note: This article is available in Chinese only. 本文暂无英文版本。 View original

http://acm.hdu.edu.cn/showproblem.php?pid=1285 题意:

有N个比赛队(1<=N<=500),编号依次为1,2,3,。。。。,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前。现在请你编程序确定排名。

Input

输入有若干组,每组中的第一行为二个数N(1<=N<=500),M;其中N表示队伍的个数,M表示接着有M行的输入数据。接下来的M行数据中,每行也有两个整数P1,P2表示即P1队赢了P2队。

拓扑排序模板题。刷dfs的时候遇到的。干脆来学习下。

注意可能有重边。

由于要求输出顺序按照序号从小到达,所以这里用了优先队列。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月08日 星期二 20时43分24秒
 4File Name :code/hdu/1285.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=5E2+7;
34int n,m;
35int in[N];
36bool con[N][N];
37priority_queue<int,vector<int>,greater<int> > q;
38void toporder()
39{
40    for ( int i = 1 ; i <= n ; i++)
41	if (in[i]==0) q.push(i);
42
43    int c = 1;
44    while (!q.empty())
45    {
46	int v =q.top();
47	q.pop();
48	if (c!=n)
49	{
50	    printf("%d ",v);
51	    c++;
52	}
53	else
54	    printf("%d\n",v);
55	for ( int i = 1 ; i <= n ; i++)
56	{
57	    if (!con[v][i])
58		continue;
59	    in[i]--;
60	    if (!in[i])
61		q.push(i);
62	}
63    }
64}
65int main()
66{
67	#ifndef  ONLINE_JUDGE
68	freopen("code/in.txt","r",stdin);
69  #endif
70
71	while (scanf("%d %d",&n,&m)!=EOF)
72	{
73	    ms(con,false);
74	    for ( int i = 0 ; i < m ; i++)
75	    {
76		int x,y;
77		scanf("%d %d",&x,&y);
78		if (con[x][y])
79		    continue;
80		con[x][y] = true;
81		in[y]++;
82
83	    }
84	    toporder();
85	}
86
87  #ifndef ONLINE_JUDGE
88  fclose(stdin);
89  #endif
90    return 0;
91}

Related

codeforces 129 B. Students and Shoelaces

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

cf 596 B. Wilbur and Array

·1 min
http://codeforces.com/problemset/problem/596/B 题意:初始序列全为0,问经过多少次变换,能变成序列b。一次变换是指,选定一个i,从i一直到最后每个元素都增加1,或者每个元素都减少1. 思路:很容易发现。后面的变换补影响前面的变换。每一个数字可以唯一由之前的增加和减少次数决定。所以我们用两个变量,记录之前做的增加和减少变换的次数。然后扫一遍即可。

codeforces 600 C. Make Palindrome

·2 mins
http://codeforces.com/problemset/problem/600/C 题意:给定一个字符串。要求用最少的变换得到一个回文串。且在变换次数相同时要字典序最小的。输出变换后的字符串。 思路:对不能构成会文串有影响的是出现奇数次的字母。所以我们先统计每个字母出现的次数。然后按照出现奇数次的字母的个数分奇偶分别搞。偶数的话直接把后面一半变成前面一半。奇数的话,也是这样。输出的时候按照字母从a扫到z,如果有就输出一半。然后再倒着扫一遍。 输出另一半。这样可以保证是字典序最小。需要注意的是奇数的时候的输出情况。不要忘记中间那个字母。

codeforces 505 B. Mr. Kitayuta's Colorful Graph

·1 min
http://codeforces.com/contest/505/problem/B 题意;给一个图,边有颜色。给q个查询,每个查询一对点x,y。问只经过某种颜色的边使得x能到y颜色数目。 思路:存颜色的时候卡了下。。本来打算开一个二维的set用来存颜色。。。没想明白。。后来发现。。还是用vecotr就好啊。。。多开一维度vector。。或者。。vector 用 pair 都是可以的。。。因为颜色数不多。。可以暴力枚举每种颜色做一遍dfs 看只走有这条颜色的边x能否到y。。