CF 915 D 拓扑排序
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int mod = 142857;
int t,n,m,k,x,u,v,w,num,flag;
vector<int> G[maxn];
int inDeg[maxn], ruDeg[maxn];
int virus[maxn];
queue<int> q;
bool topSort()
{
num=0;
while(!q.empty()) q.pop();
for(int i=1;i<=n;i++) if(!inDeg[i]) q.push(i);
while(!q.empty())
{
int now = q.front();
q.pop();
num++;
for(int i=0;i<G[now].size();i++)
{
int nxt = G[now][i];
if(--inDeg[nxt] == 0) q.push(nxt);
}
}
if(num == n) return true;
else return false;
}
int main()
{
while(~ scanf("%d%d",&n,&m) )
{
memset(inDeg,0,sizeof(inDeg));
memset(ruDeg,0,sizeof(ruDeg));
for(int i=1;i<=n;i++) G[i].clear();
while(m--)
{
scanf("%d%d",&u,&v);
G[u].push_back(v);
inDeg[v]++;
ruDeg[v]++;
}
if(topSort())
{
puts("YES");
continue;
}
else
{
flag = 0;
for(int i=1; i<=n; i++)
{
memcpy(inDeg,ruDeg,sizeof(ruDeg));
if(inDeg[i] >= 1)
{
inDeg[i]--;
if(topSort())
{
flag = 1;
puts("YES");
break;
}
}
}
}
if(flag == 0) puts("NO");
}
}
CF 915 D 拓扑排序的更多相关文章
- [CF #290-C] Fox And Names (拓扑排序)
题目链接:http://codeforces.com/contest/510/problem/C 题目大意:构造一个字母表,使得按照你的字母表能够满足输入的是按照字典序排下来. 递归建图:竖着切下来, ...
- CF Fox And Names (拓扑排序)
Fox And Names time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- CF #CROC 2016 - Elimination Round D. Robot Rapping Results Report 二分+拓扑排序
题目链接:http://codeforces.com/contest/655/problem/D 大意是给若干对偏序,问最少需要前多少对关系,可以确定所有的大小关系. 解法是二分答案,利用拓扑排序看是 ...
- CF 274D Lovely Matrix 拓扑排序,缩点 难度:2
http://codeforces.com/problemset/problem/274/D 这道题解题思路: 对每一行统计,以小值列作为弧尾,大值列作为弧头,(-1除外,不连弧),对得到的图做拓扑排 ...
- 【CodeForces】915 D. Almost Acyclic Graph 拓扑排序找环
[题目]D. Almost Acyclic Graph [题意]给定n个点的有向图(无重边),问能否删除一条边使得全图无环.n<=500,m<=10^5. [算法]拓扑排序 [题解]找到一 ...
- CF思维联系--CodeForces -214C (拓扑排序+思维+贪心)
ACM思维题训练集合 Furik and Rubik love playing computer games. Furik has recently found a new game that gre ...
- Java排序算法——拓扑排序
package graph; import java.util.LinkedList; import java.util.Queue; import thinkinjava.net.mindview. ...
- CF1131D Gourmet choice(并查集,拓扑排序)
这题CF给的难度是2000,但我感觉没这么高啊…… 题目链接:CF原网 题目大意:有两个正整数序列 $a,b$,长度分别为 $n,m$.给出所有 $a_i$ 和 $b_j(1\le i\le n,1\ ...
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) E. Tree Folding 拓扑排序
E. Tree Folding 题目连接: http://codeforces.com/contest/765/problem/E Description Vanya wants to minimiz ...
随机推荐
- [Leetcode] search a 2d matrix 搜索二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- BZOJ1982 [Spoj 2021]Moving Pebbles 【博弈论】
题目 Moving Pebbles Two players play the following game. At the beginning of the game they start with ...
- Fragmenttabhost的使用教程
1.准备tab的图标,放到mipmap目录下面,大小64x64,准备2种,一种是选中的,一种是未选中的,如下图 2.重写fragmentabhost,防止调用fragment每次点击tab都要重新调用 ...
- mybatis的mapper文件的大于号特殊符号使用
第一种方法: 用了转义字符把>和<替换掉,然后就没有问题了. SELECT * FROM test WHERE 1 = 1 AND start_date <= CURRENT_DA ...
- Java多线程1:Java中sleep,wait,yield,join的区别
1.sleep()方法 在指定时间内让当前正在执行的线程暂停执行,但不会释放“锁标志”.不推荐使用. sleep()使当前线程进入阻塞状态,在指定时间内不会执行. 2.wait()方法 在其他线程调用 ...
- shell分发文件脚本
配置文件scp.conf ssh_hosts=("IP") #需要分发机器的所有IP ssh_ports=("22") ssh_users=("roo ...
- PCIe 调试
ISE 生成PCIe核之后, 在ipcore_dir目录下会产生以下文件目录 目录下包含内容如下: The doc folder contains the PCIe Endpoint Block da ...
- 【Foreign】无聊的计算姬 [Lucas][BSGS]
无聊的计算姬 Time Limit: 10 Sec Memory Limit: 256 MB Description Input Output Sample Input 6 2 2 3 4 3 2 ...
- MyBatis系列三 之 使用getMapper剔除掉Dao的实现类
MyBatis系列三 之 使用getMapper剔除掉Dao的实现类 我们在系列一 中 我们使用的是Dao的实现类 来操作底层数据库,今天我们使用getMapper()来替换Dao的实现类, ...
- MyBatis系列二 之 数据库列名于程序实体类中字段名称不一致
MyBatis系列二 之 数据库列名于程序实体类中字段名称不一致 情景:当数据库中的列名与我们程序实体类中的字段名称不一致 使用ResultMap节点配置信息 在映射文件中 ...