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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值