Codeforces Global Round 8 E. Ski Accidents (思维)

E. Ski Accidents

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Arthur owns a ski resort on a mountain. There are nn landing spots on the mountain numbered from 11 to nn from the top to the foot of the mountain. The spots are connected with one-directional ski tracks. All tracks go towards the foot of the mountain, so there are no directed cycles formed by the tracks. There are at most two tracks leaving each spot, but many tracks may enter the same spot.

A skier can start skiing from one spot and stop in another spot if there is a sequence of tracks that lead from the starting spot and end in the ending spot. Unfortunately, recently there were many accidents, because the structure of the resort allows a skier to go through dangerous paths, by reaching high speed and endangering himself and the other customers. Here, a path is called dangerous, if it consists of at least two tracks.

Arthur wants to secure his customers by closing some of the spots in a way that there are no dangerous paths in the resort. When a spot is closed, all tracks entering and leaving that spot become unusable.

Formally, after closing some of the spots, there should not be a path that consists of two or more tracks.

Arthur doesn't want to close too many spots. He will be happy to find any way to close at most 47n47n spots so that the remaining part is safe. Help him find any suitable way to do so.

Input

The first line contains a single positive integer TT — the number of test cases. TT test case description follows.

The first line of each description contains two integers nn and mm (1≤n≤2⋅1051≤n≤2⋅105) — the number of landing spots and tracks respectively.

The following mm lines describe the tracks. Each of these lines contains two integers xx and yy (1≤x<y≤n1≤x<y≤n) — indices of the starting and finishing spots for the respective track. It is guaranteed that at most two tracks start at each spot. There may be tracks in which starting and finishing spots both coincide.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, print a single integer kk (0≤k≤47n0≤k≤47n) — the number of spots to be closed. In the next line, print kk distinct integers — indices of all spots to be closed, in any order.

If there are several answers, you may output any of them. Note that you don't have to minimize kk. It can be shown that a suitable answer always exists.

Example

input

Copy

2
4 6
1 2
1 3
2 3
2 4
3 4
3 4
7 6
1 2
1 3
2 4
2 5
3 6
3 7

output

Copy

2
3 4 
4
4 5 6 7 

Note

In the first sample case, closing any two spots is suitable.

In the second sample case, closing only the spot 11 is also suitable.

题意:

给你n(<=2e5)个点和m(?范围没给)条有向边,你需要删除最多4*n/7个点,使得没有任何点可以沿着边走两步。

保证每个点的出度不超过2。

思路:

看了巨巨的博客以后,震惊了。。因为是4/7,所以推出来深度为拓扑序里3的倍数的顶点?。。。

考虑用拓扑序计算每个结点的深度,只需要将深度为3的倍数的结点删除即可。。。

最坏情况下是:给的有向树为满二叉树并且层数为3的倍数。这时候需要删除的点的个数占总个数的比例为

\frac{2^{2}+2^{5}+...+2^{3n-1}}{2^{0}+2^{1}+...+2^{3n-1}}

运用等比数列求和公式可得比例为4/7。

需要注意的是,我们直接每一次都删除第三层的点,删除的点不在用来计算后面的边的深度。

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define mst(head,x,n) memset(head+1,x,n*sizeof(head[0]))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dep(i,a,b) for(int i=(a);i>=(b);i--)
using namespace std;
const int maxn=4e5+5;
//const double pi=acos(-1.0);
//const double eps=1e-9;
//const ll mo=1e9+7;
int n,m,k;
int a[maxn],c[maxn];
int tmp,cnt;
int flag;
char s[maxn];
bool ok[maxn];
vector<int>vc[maxn],ans;
template <typename T>
inline void read(T &X){
    X=0;int w=0; char ch=0;
    while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
    while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    if(w) X=-X;
}
int main(){
/*
#ifdef ONLINE_JUDGE
#else
    freopen("D:/Temp/in.txt", "r", stdin);
#endif
*/
    int T,cas=1;
    read(T);
    while(T--)
    //while(scanf("%d",&n)!=EOF)
    {
        read(n);read(m);
        int num=4*n/7;
        rep(i,0,n) {
            ok[i]=false;
            c[i]=0;
            vc[i].clear();
        }
        rep(i,1,m){
            int x,y;
            read(x);read(y);
            vc[x].push_back(y);
            //vc[y].push_back(x);
        }
        ans.clear();
        rep(i,1,n){
            if(c[i]%3==2) {ans.push_back(i);continue;}
            for(int j=0;j<vc[i].size();j++){
                int v=vc[i][j];
                c[v]=max(c[v],c[i]+1);
            }
        }

        int k=ans.size();
        if(k>num) puts("WA");
        printf("%d\n",k);
        rep(i,0,k-1)
        printf("%d%c",ans[i],i==k-1?'\n':' ');
    }
    return 0;
}
/*
freopen("e:\\duipai\\data.txt","r",stdin);
freopen("e:\\duipai\\myout.txt","w",stdout);
*/

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章