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);
*/

 

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