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];
int flag[maxn];

void dfs(int x)
{
    flag[x]=1;
    for(int i=0;i<g[x].size();i++)
    {
        if(flag[g[x][i]]==1) continue;
        dfs(g[x][i]);
    }printf("%d ",x);
}

int main()
{
    scanf("%d%d",&n,&m);
    memset(flag,0,sizeof flag);
    for(int i=1;i<=m;i++)
    {
        int u,v; scanf("%d%d",&u,&v);
        g[u].push_back(v);
    }

    for(int i=1;i<=n;i++) if(flag[i]==0) dfs(i);
    printf("\n");
    return 0;
}
时间: 2024-10-11 14:07:10

CodeForces 412D Giving Awards的相关文章

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>

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 手速题

//CodeForces 412D 1 #include "iostream" 2 #include "cstdio" 3 #include "cstring" 4 #include "algorithm" 5 #include "vector" 6 using namespace std; 7 int tot, first[30010], next[100010], to[100010]; 8 bool

CF 412 D Giving Awards(拓扑排序)

The employees of the R1 company often spend time together: they watch football, they go camping, they solve contests. So, it's no big deal that sometimes someone pays for someone else. Today is the day of giving out money rewards. The R1 company CEO

大神刷题表

9月27日 后缀数组:[wikioi3160]最长公共子串 dp:NOIP2001统计单词个数 后缀自动机:[spoj1812]Longest Common Substring II [wikioi3160]最长公共子串 [spoj7258]Lexicographical Substring Search 扫描线+set:[poj2932]Coneology 扫描线+set+树上删边游戏:[FJOI2013]圆形游戏 结论:[bzoj3706][FJ2014集训]反色刷 最小环:[poj1734

牛津高中英语模块8Reading课文

Unit 1 Appreciating literature What is classic literature? Classics are the antiques of the literary world. They are novels, plays and poems that were written a long time ago and were so well written and well received that people still read them toda

Codeforces Round #416 (Div. 2) A+B

A. Vladik and Courtesy 2 seconds 256 megabytes At regular competition Vladik and Valera won a and b candies respectively. Vladik offered 1 his candy to Valera. After that Valera gave Vladik 2 his candies, so that no one thought that he was less gener

codeforces Hill Number 数位dp

http://www.codeforces.com/gym/100827/attachments Hill Number Time Limits:  5000 MS   Memory Limits:  200000 KB 64-bit interger IO format:  %lld   Java class name:  Main Description A Hill Number is a number whose digits possibly rise and then possibl

Codeforces Round #200 (Div. 2) (ABCDE题解)

比赛链接:http://codeforces.com/contest/344 A. Magnets time limit per test:1 second memory limit per test:256 megabytes Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets inst