题面:
链接:https://www.luogu.com.cn/problem/P3916
思路:反向遍历图
啊卡了好久,如果正序来的话还得考虑环和先后次序的问题
代码:
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<cmath>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
#include<set>
typedef long long ll;
using namespace std;
const int N = 1e5 + 10;
vector<int>tonxt[N];
int maxn[N];
bool vis[N];
bool visbegin[N];
void dfs(int i,int goal)
{
if (vis[i])return;
vis[i] = true;
maxn[i] = goal;
for (int k = 0; k < tonxt[i].size(); k++)
{
dfs(tonxt[i][k], goal);
}
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, m; cin >> n >> m;
for (int i = 0; i < m; i++)
{
int from, to;
cin >> from >> to;
tonxt[to].push_back(from);
}
for (int i = n; i >= 1; i--)
dfs(i,i);
for (int i = 1; i <= n; i++)cout << maxn[i] << ' ';
return 0;
}
标签:遍历,goal,int,dfs,P3916,vis,include
From: https://www.cnblogs.com/zzzsacmblog/p/18180326