Codeforces D. Giving Awards 412 题解

就是按照一定顺序输出排序。

比如a欠b的钱就不能先输出a然后输出b。

本题的技巧就是,要求的是不能先输出a然后输出b,但是可以先输出b然后输出a。

故此可以按照a欠b的钱的关系,建立图,然后DFS深度优先搜索,然后逆向记录点,输出这些逆向点,也就是a欠b的钱,就先输出b然后输出a,那么这个顺序就满足要求了。

很狡猾的题意。要细心。不然就搞半天都白搞了。

题目连接:http://codeforces.com/problemset/problem/412/D

#include <stdio.h>
#include <vector>
using std::vector;

const int MAX_S = 30001;
vector<int> gra[MAX_S];
bool vis[MAX_S] = {0};
vector<int> res;
int N, M;

void dfsReverseNodes(int u)
{
	vis[u] = true;
	for (int i = 0; i < (int)gra[u].size(); i++)
	{
		int v = gra[u][i];
		if (!vis[v]) dfsReverseNodes(v);
	}
	res.push_back(u);
}

int main()
{
	int u, v;
	scanf("%d %d", &N, &M);
	for (int i = 1; i <= M; i++)
	{
		scanf("%d %d", &u, &v);
		gra[u].push_back(v);
	}
	for (int i = 1; i <= N; i++)
	{
		if (!vis[i]) dfsReverseNodes(i);
	}

	for (int i = 0; i < (int)res.size(); i++)
	{
		printf("%d ", res[i]);
	}
	return 0;
}

Codeforces D. Giving Awards 412 题解

时间: 2024-10-29 19:10:26

Codeforces D. Giving Awards 412 题解的相关文章

Codeforces D. Giving Awards 412(DFS)

题意: 给出n  , m   然后给出m组关系,  表示前者不能出现在后者的前方,  即 a  b  不行 但是  b a 是可以的 然后构建出一个序列  满足所有关系. 题解: 由题可知  ,  需要满足关系,  直接构图,  然后DFS  ,  然后反向输出答案  然后没了. 代码: #include<stdio.h> #include<iostream> #include<vector> #include<string.h> using namespa

CodeForces 412D Giving Awards

根据给出的条件建边,然后进行dfs 对于某个点x,当x的后继都遍历完毕后,再输出x节点. 这样能保证所有约束条件. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; const int maxn=30000+10; int n,m; vector<int>g[maxn]

Codeforces A. Valera and X 题解

判断二维字符串是否满足下面条件: on both diagonals of the square paper all letters are the same; all other squares of the paper (they are not on the diagonals) contain the same letter that is different from the letters on the diagonals. Help Valera, write the progra

Codeforces Round #262 (Div. 2) 题解

A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Vasya has n pairs of socks. In the morning of each day Vasya has to put on a pair of socks before he goes to school. When

codeforces A. Shaass and Oskols 题解

Shaass has decided to hunt some birds. There are n horizontal electricity wires aligned parallel to each other. Wires are numbered 1 to n from top to bottom. On each wire there are some oskols sitting next to each other. Oskol is the name of a delici

Codeforces Round #FF (Div. 2) 题解

比赛链接:http://codeforces.com/contest/447 A. DZY Loves Hash time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output DZY has a hash table with p buckets, numbered from 0 to p?-?1. He wants to insert n 

codeforces A. Slightly Decreasing Permutations 题解

Permutation p is an ordered set of integers p1,??p2,??...,??pn, consisting of n distinct positive integers, each of them doesn't exceed n. We'll denote the i-th element of permutation p as pi. We'll call number n the size or the length of permutation

Codeforces Round #259 (Div. 2) 题解

A. Little Pony and Crystal Mine time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Twilight Sparkle once got a crystal from the Crystal Mine. A crystal of size n (n is odd; n?>?1) is an n?×?n 

codeforces A. Valera and Plates 题解

Valera is a lazy student. He has m clean bowls and k clean plates. Valera has made an eating plan for the next n days. As Valera is lazy, he will eat exactly one dish per day. At that, in order to eat a dish, he needs exactly one clean plate or bowl.