Educational Codeforces Round 36 (Rated for Div. 2) ---d

D. Almost Acyclic Graph

首先判环可以用拓扑来实现。

暴力解法自然是枚举每一条边,删除,判断是否存在环。

解法一:

对于指向同一个点的边,在拓扑排序中看删除他们事实上是等价的,即那个点入度-1,那么我们枚举所有的点即可。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int inf=1e5+10;
 4 int n,m;
 5 int tot,fi[inf],nxt[inf],to[inf],in[inf];
 6 void link(int x,int y){
 7     to[++tot]=y;nxt[tot]=fi[x];fi[x]=tot;
 8 }
 9 int que[inf],l,r;
10 int tmp[inf];
11 bool topo(){
12     l=1;r=0;
13     for(int i=1;i<=n;i++)tmp[i]=in[i];
14     for(int i=1;i<=n;i++)
15         if(!tmp[i])que[++r]=i;
16     while(l<=r){
17         int u=que[l++];
18         for(int i=fi[u];i;i=nxt[i]){
19             tmp[to[i]]--;
20             if(!tmp[to[i]])que[++r]=to[i];
21         }
22     }
23     return r==n;
24 }
25 int main()
26 {
27     scanf("%d%d",&n,&m);
28     for(int i=1;i<=m;i++){
29         int x,y;
30         scanf("%d%d",&x,&y);
31         link(x,y);
32         in[y]++;
33     }
34     for(int i=1;i<=n;i++){
35         if(!in[i])continue;
36         in[i]--;
37         if(topo()){
38             cout<<"YES"<<endl;
39             return 0;
40         }
41         in[i]++;
42     }
43     cout<<"NO"<<endl;
44     return 0;
45 }

解法二:

我们先随便找出一个简单环,那么如果有解一定是这个环上的某个边,枚举判断即可。环的大小自然是On级别的。通过dfs可以找出。

注意环不一定存在。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int inf=1e5+10;
 4 int n,m;
 5 int tot,fi[inf],nxt[inf],to[inf],in[inf],flag[inf];
 6 void link(int x,int y){
 7     to[++tot]=y;nxt[tot]=fi[x];fi[x]=tot;
 8 }
 9 int que[inf],l,r;
10 int tmp[inf];
11 bool topo(){
12     l=1;r=0;
13     for(int i=1;i<=n;i++)tmp[i]=in[i];
14     for(int i=1;i<=n;i++)
15         if(!tmp[i])que[++r]=i;
16     while(l<=r){
17         int u=que[l++];
18         for(int i=fi[u];i;i=nxt[i]){
19             if(flag[i])continue;
20             tmp[to[i]]--;
21             if(!tmp[to[i]])que[++r]=to[i];
22         }
23     }
24     return r==n;
25 }
26 int vis[inf];
27 int sta[inf],top;
28 void dfs(int x){
29     vis[x]=1;
30     for(int i=fi[x];i;i=nxt[i]){
31         if(!vis[to[i]]){
32             sta[++top]=i;
33             dfs(to[i]);
34             top--;
35         }
36         if(vis[to[i]]==1){
37             sta[++top]=i;
38             for(int j=1;j<=top;j++){
39                 flag[sta[j]]=1;
40                 in[to[sta[j]]]--;
41                 if(topo()){
42                     cout<<"YES"<<endl;
43                     exit(0);
44                 }
45                 in[to[sta[j]]]++;
46                 flag[sta[j]]=0;
47             }
48             cout<<"NO"<<endl;
49             exit(0);
50         }
51     }
52     vis[x]=-1;
53 }
54 int main()
55 {
56     scanf("%d%d",&n,&m);
57     for(int i=1;i<=m;i++){
58         int x,y;
59         scanf("%d%d",&x,&y);
60         link(x,y);
61         in[y]++;
62     }
63     for(int i=1;i<=n;i++){
64         dfs(i);
65     }
66     cout<<"YES"<<endl;
67     return 0;
68 }

原文地址:https://www.cnblogs.com/hyghb/p/8505620.html

时间: 2024-11-07 20:31:25

Educational Codeforces Round 36 (Rated for Div. 2) ---d的相关文章

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons(动态开点线段树)

链接: https://codeforces.com/problemset/problem/915/E 题意: This year Alex has finished school, and now he is a first-year student of Berland State University. For him it was a total surprise that even though he studies programming, he still has to atten

Educational Codeforces Round 36 (Rated for Div. 2) G. Coprime Arrays

求a_i 在 [1,k]范围内,gcd(a_1,a_2...,a_n) = 1的a的数组个数. F(x)表示gcd(a_1,a_2,...,a_n) = i的a的个数 f(x)表示gcd(a_1,a_2,...,a_n) = ki的a的个数(实际上就是i的倍数) f(x) = segma(x | d) F(d) F(x) = segma(x | d) mu(d / x) * f(d) F(1) = segma(d,1,k) mu(d) * f(d) f(d) = (k / d)^n 由于k变化时

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers

原文链接:https://www.cnblogs.com/xwl3109377858/p/11404050.html Educational Codeforces Round 71 (Rated for Div. 2) A - There Are Two Types Of Burgers There are two types of burgers in your restaurant — hamburgers and chicken burgers! To assemble a hamburg

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm