为什么 1∼N 枚举 TLE 90pts,N∼1 枚举 AC
TLE 90pts:
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define ul unsigned long long
using namespace std;
vector<int> v[114514];
int A[1919810];
void dfs(int p, int w) {
if (A[p] >= w) return;
A[p] = w;
for (auto i : v[p])
dfs(i, w);
}
int main() {
int N, M;
scanf ("%d%d", &N, &M);
for (int i = 1; i <= M; ++i) {
int x, y;
scanf ("%d%d", &x, &y);
v[y].push_back(x);
}
for (int i = 1; i <= N; ++i) dfs(i, i);
for (int i = 1; i <= N; ++i) printf ("%d ", A[i]);
}
AC:
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define ul unsigned long long
using namespace std;
vector<int> v[114514];
int A[1919810];
void dfs(int p, int w) {
if (A[p]) return;
A[p] = w;
for (auto i : v[p])
dfs(i, w);
}
int main() {
int N, M;
scanf ("%d%d", &N, &M);
for (int i = 1; i <= M; ++i) {
int x, y;
scanf ("%d%d", &x, &y);
v[y].push_back(x);
}
for (int i = N; i >= 1; --i) dfs(i, i);
for (int i = 1; i <= N; ++i) printf ("%d ", A[i]);
}