Codeforces-1062F:Upgrading Cities(拓扑排序)

本文探讨了在一个由多个城市组成的王国中,如何通过识别重要和半重要城市来优化旅行系统。重要城市是指可以到达所有其他城市的地点,而半重要城市则是在移除一个非本身的城市后能成为重要城市的位置。文章提出了一种算法,通过拓扑排序确定每个城市能够到达的其他城市数量,为国王提供升级哪些城市的决策依据。

F. Upgrading Cities
time limit per test 2 seconds
memory limit per test 256 megabytes
inputstandard input
outputstandard output

There are n cities in the kingdom X, numbered from 1 through n. People travel between cities by some one-way roads. As a passenger, JATC finds it weird that from any city u, he can’t start a trip in it and then return back to it using the roads of the kingdom. That is, the kingdom can be viewed as an acyclic graph.

Being annoyed by the traveling system, JATC decides to meet the king and ask him to do something. In response, the king says that he will upgrade some cities to make it easier to travel. Because of the budget, the king will only upgrade those cities that are important or semi-important. A city u is called important if for every city v≠uv≠uv̸=u, there is either a path from u to v or a path from v to u. A city u is called semi-important if it is not important and we can destroy exactly one city v≠uv≠uv̸=u so that u becomes important.

The king will start to act as soon as he finds out all those cities. Please help him to speed up the process.

Input
The first line of the input contains two integers n and m (2≤n≤300000,1≤m≤300000)(2≤n≤300000, 1≤m≤300000)(2n300000,1m300000) — the number of cities and the number of one-way roads.

Next m lines describe the road system of the kingdom. Each of them contains two integers uiu_iui and viv_ivi (1≤ui,vi≤n,ui≠vi)(1≤u_i,v_i≤n, u_i≠v_i)(1ui,vin,ui̸=vi), denoting one-way road from uiu_iui to viv_ivi.

It is guaranteed, that the kingdoms’ roads make an acyclic graph, which doesn’t contain multiple edges and self-loops.

Output
Print a single integer — the number of cities that the king has to upgrade.

Examples
input
7 7
1 2
2 3
3 4
4 7
2 5
5 4
6 4
output
4
input
6 7
1 2
2 3
3 4
1 5
5 3
2 6
6 4
output
4

思路:题意很清楚,只要对于每个点,求出能到达它的点数和它能到达的点数之和。

考虑拓扑排序的过程。

用一个变量tot记录当前还未访问的点的数量,当队列中只有一个点时,那么这个点肯定能到达剩下的tot个点,将tot记录下来;若队列中有2个点,那么判断当前出队列的这个点的儿子是否包含另一个点的所有儿子,如果是,那么这个点能到达剩下的tot个点,否则到达不了,那对答案就没有贡献,所以直接舍弃即可;若队列中的点不止2个,那么这些点肯定也都对答案没有贡献,直接舍弃即可。

因为要求出能到达它的点数和它能到达的点数,所以还需将所有的边反向再来一次。

最后判断对于每个点记录下来的点数是否大于等n-2。

#include<bits/stdc++.h>
using namespace std;
const int MAX=3e5+10;
vector<int>e[MAX];
int in[MAX];
int d[MAX];
int n,m;
void cal(int *u,int *v)
{
    memset(in,0,sizeof in);
    for(int i=1;i<=n;i++)e[i].clear();
    for(int i=1;i<=m;i++)
    {
        e[u[i]].push_back(v[i]);
        in[v[i]]++;
    }
    int tot=n;
    queue<int>p;
    for(int i=1;i<=n;i++)if(in[i]==0)p.push(i),tot--;
    while(!p.empty())
    {
        int x=p.front();p.pop();
        if(p.empty())d[x]+=tot;
        if(p.size()==1)
        {
            int c=1;
            for(int i=0,y=p.front();i<e[y].size();i++)c&=(in[e[y][i]]>1);
            d[x]+=c*tot;
        }
        for(int i=0;i<e[x].size();i++)if(!--in[e[x][i]]){p.push(e[x][i]),tot--;}
    }
}
int u[MAX],v[MAX];
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)scanf("%d%d",&u[i],&v[i]);
    cal(u,v);
    cal(v,u);
    int ans=0;
    for(int i=1;i<=n;i++)ans+=(d[i]>=n-2);
    cout<<ans<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值