I - 排名表

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit Status

暑假前集训已经过了一半了,我们将会把当前排名公布出来。但是此刻秋实大哥却心急火燎,因为他不慎把排名删除了。

一共有n个人参加排名,每个人都有一个名次,没有哪两个人的名次是相同的。现在秋实大哥掌握的一些情报,比如Ai的名次要先于Bi。(编号从1开始)

你能帮秋实大哥恢复出排名表吗?

Input

第一行一个数字 T (T≤10),表示测试数据组数

每组测试数据,第一行两个数 n(1≤n≤200)和 m(0≤m≤40000),接下来m行,每行两个数a和b(1≤a,b≤N),表示a的名次要先于b

Output

对于每组测试数据,输出一行,从1号到n号每个人的名次。

如果有多个解,让编号为1的人的名次尽量小,然后让编号为2的人的名次尽量小,然后让编号为3的人的名次尽量小......

如果没有解,输出−1

Sample input and output

Sample Input Sample Output
5
4 0
4 1
1 1
4 2
1 2
2 1
4 1
2 1
4 1
3 2
1 2 3 4
-1
-1
2 1 3 4
1 3 2 4

Hint

注意可能会有重边

解题思路:

首先,本题正向拓扑排序是不行的,即如果这样建边:

U 的名次先于 V , 即 V 向 U连边

因为题意比较绕,下文都这样叙述:

排名号越高,其名次越低,第一名的排名号是 1 ,它的名次是最高的.

我们按照 v 向 u建边,意思是v的排名号高于u.

无法保证

让编号为1的人的名次尽量小,然后让编号为2的人的名次尽量小,然后让编号为3的人的名次尽量小.

è 让编号为 1 的人的排名号尽量大,然后让编号为 2 的人的排名号尽量大….

我们拓扑排序的顺序是这样的:

每次寻找入度为 0 的点,将排名号赋给这个点,删边,重复.

如果有多个点入度都是 0 呢?我们不假思索的这样想:

 我们为了保证题目条件,会使得我们会尽量保证这个点的编号尽量小(这样我们看起来符合了题目条件:编号小的人排名号尽量高),但是我们忽略了,题目要求是先尽力保证编号1的人,之后才是编号2的人….

所以,正向建边是错误的

注意,上面的证明非常不严格(Even it's a mistake),如果小伙伴谁有更好的证明,可在下面回复

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#define pb push_back
using namespace std;
const int maxn = + ;
int n,m,c[maxn],indig[maxn];
vector<int>E[maxn];
bool existedge[maxn][maxn];
int out[maxn]; bool dfs(int cur)
{
if (c[cur] == )
return true;
c[cur] = -;
for(int i = ; i < E[cur].size() ; ++ i)
{
int nextnode = E[cur][i];
if (c[nextnode] == -)
return false;
if (!c[nextnode] && !dfs(nextnode)) //exist 环
return false;
}
c[cur] = ;
return true;
} typedef struct point
{
int id,indig;
friend bool operator < (const point & x,const point & y)
{
return x.indig < y.indig;
}
point(int id,int indig)
{
this->id = id , this->indig = indig;
}
}; vector<point>v;
bool used[maxn]; void ansset()
{
memset(used,false,sizeof(used));
for(int i = ; i <= n ; ++ i)
v.pb(point(i,indig[i]));
sort(v.begin(),v.end());
int rank = ;
for(int i = ; i <= n ; ++ i)
{
int choose = v[].id;
int pos = ;
while( pos < v.size() && !v[pos].indig)
choose = max(choose,v[pos++].id);
out[choose] = n - rank++;
used[choose] = true;
for(int i = ; i < E[choose].size() ; ++ i)
indig[E[choose][i]]--;
v.clear();
for(int i = ; i <= n ; ++ i)
if(!used[i])
v.pb(point(i,indig[i]));
sort(v.begin(),v.end());
}
} int main(int argc,char *argv[])
{
int Case;
scanf("%d",&Case);
while(Case--)
{
scanf("%d%d",&n,&m);
memset(c,,sizeof(c));
memset(existedge,false,sizeof(existedge));
memset(indig,,sizeof(indig));
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
if (existedge[v][u])
continue;
E[v].pb(u);
existedge[v][u] = true;
indig[u] ++;
}
int ans = ;
for(int i = ; i <= n ; ++ i)
if (!dfs(i))
{
ans = ;
break;
}
if (!ans)
printf("-1\n");
else
{
v.clear();
ansset();
for(int i = ; i <= n ; ++ i)
i == ? printf("%d",out[i]) : printf(" %d",out[i]);
printf("\n");
}
for(int i = ; i <= n ; ++ i)
E[i].clear();
}
return ;
}

UESTC_排名表 2015 UESTC Training for Graph Theory<Problem I>的更多相关文章

  1. UESTC_传输数据 2015 UESTC Training for Graph Theory<Problem F>

    F - 传输数据 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  ...

  2. UESTC_方老师和农场 2015 UESTC Training for Graph Theory<Problem L>

    L - 方老师和农场 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  3. UESTC_王之盛宴 2015 UESTC Training for Graph Theory<Problem K>

    K - 王之盛宴 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  ...

  4. UESTC_小panpan学图论 2015 UESTC Training for Graph Theory<Problem J>

    J - 小panpan学图论 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) S ...

  5. UESTC_韩爷的情书 2015 UESTC Training for Graph Theory<Problem H>

    H - 韩爷的情书 Time Limit: 6000/2000MS (Java/Others)     Memory Limit: 262144/262144KB (Java/Others) Subm ...

  6. UESTC_树上的距离 2015 UESTC Training for Graph Theory<Problem E>

    E - 树上的距离 Time Limit: 2000/1000MS (Java/Others)     Memory Limit: 262143/262143KB (Java/Others) Subm ...

  7. UESTC_邱老师的脑残粉 2015 UESTC Training for Graph Theory<Problem D>

    D - 邱老师的脑残粉 Time Limit: 12000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  8. UESTC_秋实大哥与时空漫游 2015 UESTC Training for Graph Theory<Problem C>

    C - 秋实大哥与时空漫游 Time Limit: 4500/1500MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Su ...

  9. UESTC_秋实大哥带我飞 2015 UESTC Training for Graph Theory<Problem B>

    B - 秋实大哥带我飞 Time Limit: 300/100MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit ...

随机推荐

  1. myeclipse实现Servlet实例(1) 通过继承servlet接口实现

    1.在myeclipse新建web project,配置Tomcat(在myeclipse的Window--preferences) 2.然后在src新建servlet文件( 此处放在com.tsin ...

  2. C# 二分查询

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. 真实经纬度(gps)转成百度坐标的js方法

    转:http://www.360doc.com/content/16/0320/14/18636294_543805051.shtml 结果图: <!DOCTYPE html> <h ...

  4. mysql 1449 : The user specified as a definer (&#39;root&#39;@&#39;%&#39;) does not exist 解决方法

    权限问题,授权 给 root  全部sql 权限 mysql> grant all privileges on *.* to root@"%" identified by & ...

  5. linux下使用mutt发送带附件的邮件

    echo "hello"|mutt -s "world" -a hack.jpg -- name@address.com

  6. [Cycle.js] The Cycle.js principle: separating logic from effects

    The guiding principle in Cycle.js is we want to separate logic from effects. This first part here wa ...

  7. 2.IKAnalyzer 中文分词器配置和使用

    一.配置 IKAnalyzer 中文分词器配置,简单,超简单. IKAnalyzer 中文分词器下载,注意版本问题,貌似出现向下不兼容的问题,solr的客户端界面Logging会提示错误. 给出我配置 ...

  8. StartService与BindService

    效果图 MainActivity.java package com.wangzhen.servicedemo; import com.lidroid.xutils.ViewUtils; import ...

  9. 在Fedora20上安装Oracle 12c

    本文将引导大家在Fedora20的环境下成功安装Oracle12c. 安装前的准备 编辑/etc/hosts文件,添加本机名称 编辑/etc/selinux/config文件 编辑/etc/redha ...

  10. Windows 不能在 本地计算机 启动 SQL Server(MSSQLSERVER)。错误码126

    结合自己的解决方案和网络上搜到的内容,现总结如下: 首先你要知道问题出在了什么地方才能针对性处理. 1.打开事件查看器 计算机右击——管理 右侧会出现错误列表,在其中找到SQL server有关的查看 ...