sgu-230 Weighings

题目大意:

给你一个n个点,m条边的有向图,然后要你求出一条经过所有点的路径,输出第i个点是第几个经过的。

解题思路:

话说这道题目我看了好久才看懂啊,毕竟英语差啊。。。。。

很水的一道题目,就是一遍拓扑排序就行了,没什么可讲的了。。。

AC代码:

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)>(b)?(b):(a))

using namespace std;
int n,m;
struct bian_
{
	int next;
	int num;
}bian[10010]={{0,0}};
int First[110]={0};
int hash[110]={0};
int du[110]={0};

inline void Add(int p,int q,int k)
{
	bian[k].next=First[p];
	bian[k].num=q;
	First[p]=k;
	return;
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		int p,q;
		scanf("%d%d",&p,&q);
		Add(p,q,i);
		du[q]++;
	}
	int dui[110]={0};
	int duip=0;
	for(int i=1;i<=n;i++)
		if(du[i]==0) dui[++duip]=i;
	for(int i=1;i<=duip;i++)
	{
		int u=dui[i];
		for(int p=First[u];p!=0;p=bian[p].next)
		{
			du[bian[p].num]--;
			if(du[bian[p].num]==0)
				dui[++duip]=bian[p].num;
		}
	}
	if(duip<n) cout<<"No solution"<<endl;
	else
	{
		int ans[110]={0};
		for(int i=1;i<=n;i++)
			ans[dui[i]]=i;
		for(int i=1;i<=n;i++)
			printf("%d ",ans[i]);
		puts("");
	}
	return 0;
}
时间: 2024-08-04 03:36:46

sgu-230 Weighings的相关文章

SGU 230. Weighings (拓扑排序)

题意: 给出质量为1~n的n个箱子的m对轻重关系,输出一种可能的箱子的质量排列. Solution: 拓扑排序,注意要处理重边. #include <iostream> #include <queue> using namespace std; const int N = 209; queue<int> q; bool G[N][N]; int deg[N], ans[N]; int n, m; int main() { ios::sync_with_stdio (0)

SGU题目总结

SGU还是个不错的题库...但是貌似水题也挺多的..有些题想出解法但是不想写代码, 就写在这里吧...不排除是我想简单想错了, 假如哪位神犇哪天发现请告诉我.. 231.Prime Sum. Find all pairs of prime numbers (A, B) such that A<=B and their sum is also a prime number and does not exceed N.1<=N<=10^6看起来挺难的样子..a prime应该是奇数(除了2)

SGU 167.I-country

时间限制:0.75s 空间限制:6M 题意: 在一个n*m(n,m<=15)的网格中,每个格子有一个值,现在从网格中取出k(k<=n*m)个,保证在选中的格子中从任意一个格子去另外的所有格子最多只用到四种(上,下,左,右)操作中的两种,并使得到的值最大.输出该值和选中的格子坐标. Solution 从选中的网格的任意一个去所有格子只用两种操作: 左图一种满足条件的网格集合,右图的(1,3)到(3,2)必需使用(左,下,右)三种操作才能到达,是不符合要求的. 注意到,满足条件的网格集合一定不会出

【SGU 390】Tickets (数位DP)

Tickets Description Conductor is quite a boring profession, as all you have to do is just to sell tickets to the passengers. So no wonder that once upon a time in a faraway galaxy one conductor decided to diversify this occupation. Now this conductor

导入BaiduMapSdkDemo报230错误解决思路

百度地图官网教程,官方论坛的指导文章都指引读者去使用默认的.android目录下的debug.keystore.然而却在官方的Demo中使用app根目录下的debug.keystore导致简单的错误一直迷路,记录一下自己的解决问题的思路.这其中暴露了我容易走弯路,并且容易偏题的毛病. 问题描述: 导入BaiduMapSdkDemo的时候,按照官方教程申请了AK.但是AS打包到真机却总是提示: key验证出错,230错误. 解决途径: 官方论坛,下载:安全码校验工具.发现SHA1指纹和我.andr

ACM: SGU 101 Domino- 欧拉回路-并查集

sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Description Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The bl

SGU 116 Index of super-prime 数论+完全背包+输出方案

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=116 题意好晦涩 给你一个不超过一万的数 问它最少可以用多少个“超级素数”来表示 使“超级素数”之和等于它 如果无法这样表示 输出0 否则 按非降序形式输出方案 数论部分就模板的问题 没什么说的 完全背包方面也很常规 说说[输出方案] 背包九讲的伪码给的是二维dp[]的方法 实际上稍加改动就可以用在一维数组上 用一个rec[]记录dp[]的当前状态是从哪个状态转移而来(即上一个状态) 通过

SGU 乱搞日志

SGU 100 A+B :太神不会 SGU 101 Domino: 题目大意:有N张骨牌,两张骨牌有两面有0到6的数字,能相连当且仅当前后数字相同,问能否有将N张骨牌连接的方案?思路:裸的欧拉回路,注意自环,连通 1 //sgu101 2 #include<iostream> 3 #include<cstdio> 4 #include <math.h> 5 #include<algorithm> 6 #include<string.h> 7 #i

SGU 275 To xor or not to xor (高斯消元)

题目地址:SGU 275 首先,贪心的思想,每一二进制位上要尽量是1,而能不能是1用高斯消元来解决.当该位有一个可以使之为1的变元时,就说明这位可以为1,而且令该变元控制该位,然后向低位消元. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h>

绑卡免费拿15元,投900元【拿230元羊毛】中国平安陆金所

2015年1月1日至31日有效(1月13日更新最新规则) http://affiliate.lufax.com/action/DnPg,点此链接注册 快讯:1仟元起投(用币后只需900元),[57天]期限,到期还本付息的[富盈人生],已经放出[第80期],1月23日14点募集结束(可能提前集满结束)大家抓紧吧~~! [关于富盈人生]富盈人生投资方向:(安全性类似于货币基金,但不能随时提现,只能到期一次性收取本息,以下copy陆金所网站)主 要投资于货币基金等货币类资产.央行票据.银行定期存款.国