C Brexit

C Brexit

C Brexit
题意:有C个国家,P条贸易关系,X是你所在的国家,L是第一个离开的国家,如果一个国家有一半的贸易关系的国家离开,这个国家也离开。

A long time ago in a galaxy far, far away, there was a large interstellar trading union, consisting
of many countries from all across the galaxy. Recently, one of the countries decided to leave
the union. As a result, other countries are thinking about leaving too, as their participation
in the union is no longer beneficial when their main trading partners are gone.
You are a concerned citizen of country X, and you want to find out whether your country will
remain in the union or not. You have crafted a list of all pairs of countries that are trading
partners of one another. If at least half of the trading partners of any given country Y leave
the union, country Y will soon follow. Given this information, you now intend to determine
whether your home country will leave the union.
Input
The input starts with one line containing four space separated integers C, P, X, and L. These
denote the total number of countries (2 ≤ C ≤ 200 000), the number of trading partnerships
(1 ≤ P ≤ 300 000), the number of your home country (1 ≤ X ≤ C) and finally the number
of the first country to leave, setting in motion a chain reaction with potentially disastrous
consequences (1 ≤ L ≤ C).
This is followed by P lines, each containing two space separated integers Ai and Bi satisfying
1 ≤ Ai < Bi ≤ C. Such a line denotes a trade partnership between countries Ai and Bi
. No
pair of countries is listed more than once.
Initially, every country has at least one trading partner in the union.
Output
For each test case, output one line containing either “leave” or “stay”, denoting whether
you home country leaves or stays in the union.
8 Problem C: Brexit
Sample Input 1 Sample Output 1
4 3 4 1 stay
2 3
2 4
1 2
Sample Input 2 Sample Output 2
5 5 1 1 leave
3 4
1 2
2 3
1 3
2 5
Sample Input 3 Sample Output 3
4 5 3 1 stay
1 2
1 3
2 3
2 4
3 4
Sample Input 4 Sample Output 4
10 14 1 10 leave
1 2
1 3
1 4
2 5
3 5
4 5
5 6
5 7
5 8
5 9
6 10
7 10
8 10
9 10

思路:用vector 和 队列写的 下面的代码也挺容易理解的

#include<bits/stdc++.h>
using namespace std;
#define maxn 300005
vector<int>g[maxn];
int vis[maxn];
int num[maxn];
int numm[maxn];

int main()
{
    ios::sync_with_stdio(0);
    int c, p, x, l, u, v;
    cin >> c >> p >> x >> l;
    for(int i = 0; i < p; i ++)
    {
        cin >> u >> v;
        g[u].push_back(v);
        num[u]++;
        g[v].push_back(u);
        num[v]++;
    }
    queue<int>go;
    go.push(l);
    vis[l] = 1;
    while(!go.empty())
    {
        int xx = go.front();
        go.pop();
        for(int i = 0; i < g[xx].size(); i ++)
        {
            int yy = g[xx][i];
            if(vis[yy]) continue;
            numm[yy]++;
            if(numm[yy]*2>=num[yy])
            {
                go.push(yy);
                vis[yy] = 1;
            }
        }
    }
    if(vis[x]==1) cout <<"leave"<< endl;
    else cout <<"stay"<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值