Monitoring Ski Paths(dfs序+搜索)

题目描述
Fresh powder on a sunny day: it is a great time to ski! Hardcore skiers flock to a large mountain in the Rockies to enjoy these perfect conditions. The only way up the mountain is by helicopter; skiers jump out and ski down the mountain.c++

This process sounds a bit chaotic, so some regulations are in place. Skiers can only enter or exit the mountain at a set of designated locations called junctions. Once on the mountain, they are only allowed to travel along designated ski paths, each of which starts at one junction and ends at another junction lower on the mountain. Multiple ski paths might start at the same junction, but no two ski paths end at the same junction to avoid collisions between skiers.web

Finally, each skier must register a ski plan a day in advance with the helicopter service. The ski plan specifies the junction they fly up to and the junction lower on the mountain where they get picked up. If a skier shows up to the mountain, they must follow their plan; but some skiers get sick and do not show up at all.app

Your job is to look through the ski plans and set up monitors at some of the junctions to count how many skiers actually show up. To keep operating costs as low as possible, you should determine the minimum number of junctions that need to be monitored so that each skier passes through at least one monitored junction.svg

Figure 1: Illustration of the first sample input.
Figure 1 shows the first sample input. The dashed lines indicate the five different plans that were registered by skiers. By monitoring junctions 5, 9, and 10, you can ensure that all plans include at least one monitored junction. Monitoring fewer functions would miss some skiers.spa

输入
The first line of input has three integers n, k, and m, where n (2≤n≤250000) is the number of junctions, k (1≤k<n) is the number of ski paths, and m (1≤m≤250000) is the number of routes.code

Then k lines follow, each containing two integers 1≤u,v≤n indicating that there is a ski path that starts at junction u and ends at junction v. No (u,v) pair appears more than once.xml

Then m lines follow, each containing two distinct integers 1≤s,t≤n. Each line indicates that a skier plans to land at junction s and ski down the mountain to junction t. It is guaranteed it is possible to reach junction t from junction s by following ski paths and that junction t is at the base of the mountain (i.e. no ski paths start at t). No (s,t) pair appears more than once.排序

输出
Output the minimum number of junctions that need to be monitored so each ski plan includes at least one monitored junction.three

样例输入
10 8 5
1 5
5 6
5 4
7 3
3 10
3 9
10 2
10 8
1 6
5 4
7 2
3 9
10 8ip

样例输出
3

思路
对每个树创建它的dfs序,记录每一个人的路径并根据起点的dfs序排序,从dfs序最大的点开始向起点递推,如存在未覆盖到终点的状况就增长一个摄像头并确认覆盖区域,便可获得答案

代码实现

#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>

using namespace std;
typedef  long long ll;
const int N  = 250010;
typedef pair<int,int>P;

int n,m,k;
int degi[N],order[N];
vector<int>g[N],gt[N];
P lok[N];
bool vis[N];
void dfs(int x,int lev)
{
    order[x]=lev;
    for(int i=0;i<g[x].size();i++) dfs(g[x][i],lev+1);
}

void mov(int x)
{
    if(vis[x]) return ;
    vis[x]=true;
    for(int i=0;i<g[x].size();i++) mov(g[x][i]);
}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0;i<m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        degi[v]++;
    }
    for(int i=1;i<=n;i++) if(!degi[i]) dfs(i,1);
    for(int i=1;i<=k;i++)
    {
        scanf("%d%d",&lok[i].first,&lok[i].second);
        gt[order[lok[i].first]].push_back(i);
    }
    int ans=0;
    for(int i=N-1;i>0;i--)
    {
        for(int j=0;j<gt[i].size();j++)
        {
            int v=lok[gt[i][j]].second,u=lok[gt[i][j]].first;
            if(vis[v]) continue;
            ans++;
            mov(u);
        }
    }
    printf("%d\n",ans);
    return 0;
}