cf915D(拓扑排序)

emmmm这题一开始想直接一条条边删然后判环,不过目测是会TLE的啦。。所以就往一些奇葩的思路想去了。。GG

其实思路已经差不多了,不过考虑到去边的目的是去环,那么可以直接找一个环在上面去边就可以了,这样边的数目最多为n,然后稠密图判环就不能太随便了,拓扑排序才是正统的方法,别忘了。。。



#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define eps 1e-8
#define inf (ll)10000000000
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define ls T[i<<1]
#define rs T[i<<1|1]
#define op T[i]
#define mid (x+y>>1)
#define NM 1005
#define nm 2000005
#define pi 3.141592653
using namespace std;
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}



int n,m,_x,_y,in[NM],_in[NM],tot;
struct edge{int t;bool v;edge*next;}e[nm],*h[NM],*o=e,*b[NM];
void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;}
bool v[NM];
queue<int >q;
bool bfs(){
	inc(i,1,n)if(!in[i])q.push(i);
	while(!q.empty()){
		int t=q.front();q.pop();
		link(t)if(!j->v){
			in[j->t]--;
			if(!in[j->t])q.push(j->t);
		}
	}
	inc(i,1,n)if(in[i])return false;
	return true;
}

bool dfs(int x){
	v[x]=true;
	link(x)if(in[j->t])
		if(v[j->t]){
			b[++tot]=j;
			return true;
		}else{
			b[++tot]=j;
			if(dfs(j->t))return true;else tot--,in[j->t]=0;
		}
	v[x]=false;
	return false;
}

int main(){
	n=read();m=read();
	inc(i,1,m){_x=read();_y=read();add(_x,_y);_in[_y]++;}
	inc(i,1,n)in[i]=_in[i];
	if(bfs()){puts("YES");return 0;}
	//inc(i,1,n)printf("%d ",in[i]);putchar('\n');
	inc(i,1,n)if(in[i])if(dfs(i))break;
	//printf("%d\n",tot);
	inc(i,1,tot){
		inc(j,1,n)in[j]=_in[j];
		b[i]->v++;in[b[i]->t]--;
		if(bfs())return 0*printf("YES\n");
		b[i]->v=false;in[b[i]->t]--;
	}
	puts("NO");
	return 0;
}




D. Almost Acyclic Graph
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a directed graph consisting of n vertices and m edges (each edge is directed, so it can be traversed in only one direction). You are allowed to remove at most one edge from it.

Can you make this graph acyclic by removing at most one edge from it? A directed graph is called acyclic iff it doesn't contain any cycle (a non-empty path that starts and ends in the same vertex).

Input

The first line contains two integers n and m (2 ≤ n ≤ 500, 1 ≤ m ≤ min(n(n - 1), 100000)) — the number of vertices and the number of edges, respectively.

Then m lines follow. Each line contains two integers u and v denoting a directed edge going from vertex u to vertex v (1 ≤ u, v ≤ n, u ≠ v). Each ordered pair (u, v) is listed at most once (there is at most one directed edge from u to v).

Output

If it is possible to make this graph acyclic by removing at most one edge, print YES. Otherwise, print NO.

Examples
Input
3 4
1 2
2 3
3 2
3 1
Output
YES
Input
5 6
1 2
2 3
3 2
3 1
2 1
4 5
Output
NO
Note

In the first example you can remove edge , and the graph becomes acyclic.

In the second example you have to remove at least two edges (for example, and ) in order to make the graph acyclic.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值