/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
*/
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define eb emplace_back
#define PI acos(-1.0)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define BIT(x, i) (1 & ((x) >> (i)))
#define MASK(x) (1LL << (x))
#define task "tnc"
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;
typedef long double ld;
const ll INF = 1e18;
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
const int mo = 998244353;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using pii = pair<pair<ll, ll>, ll>;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
int n, m;
struct Edge
{
int a, b, r, p;
bool operator<(const Edge &x)
{
return r < x.r;
}
} ind[maxn];
vector<int> G[maxn];
int deg[maxn];
int ans[maxn];
bool ok[maxn];
signed main()
{
cin.tie(0), cout.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
cin >> ind[i].a >> ind[i].b >> ind[i].r >> ind[i].p;
}
sort(ind + 1, ind + 1 + m);
for (int i = 1; i <= m; i++)
{
G[ind[i].b].pb(i);
deg[ind[i].a]++;
}
queue<int> qu;
for (int i = 1; i <= n; i++)
{
ans[i] = -2;
if (deg[i] == 0)
{
qu.push(i);
}
}
while (qu.size())
{
int u = qu.front();
qu.pop();
ans[u] = -1;
for (auto v : G[u])
{
deg[ind[v].a]--;
ok[v] = 1;
if (deg[ind[v].a] == 0)
{
qu.push(ind[v].a);
}
}
}
for (int i = m; i >= 1; i--)
{
if (ok[i] == 0)
{
ok[i]=1;
deg[ind[i].a]--;
if (ans[ind[i].a] == -2)
{
ans[ind[i].a] = ind[i].r;
}
else
{
ans[ind[i].a] = min(ans[ind[i].a], ind[i].r);
}
if (deg[ind[i].a] == 0)
{
qu.push(ind[i].a);
while (qu.size())
{
int u = qu.front();
qu.pop();
// ans[u]=ind[i].r;
for (auto v : G[u])
{
if(ok[v]==0){
if (ans[ind[v].a] == -2)
{
ans[ind[v].a] = max(ans[u] - ind[v].p, ind[v].r);
}
else
{
ans[ind[v].a] = min(ans[ind[v].a], max(ans[u] - ind[v].p, ind[v].r));
}
deg[ind[v].a]--;
ok[v] = 1;
if (deg[ind[v].a] == 0)
{
qu.push(ind[v].a);
}
}
}
}
}
}
}
for (int i = 1; i <= n; i++)
{
cout << ans[i] << " ";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23892 KB |
Output is correct |
2 |
Correct |
12 ms |
23960 KB |
Output is correct |
3 |
Correct |
12 ms |
23812 KB |
Output is correct |
4 |
Correct |
12 ms |
23920 KB |
Output is correct |
5 |
Correct |
12 ms |
23892 KB |
Output is correct |
6 |
Correct |
12 ms |
23964 KB |
Output is correct |
7 |
Correct |
12 ms |
23836 KB |
Output is correct |
8 |
Correct |
12 ms |
23892 KB |
Output is correct |
9 |
Correct |
12 ms |
23892 KB |
Output is correct |
10 |
Correct |
12 ms |
23840 KB |
Output is correct |
11 |
Correct |
12 ms |
23932 KB |
Output is correct |
12 |
Correct |
12 ms |
23792 KB |
Output is correct |
13 |
Correct |
12 ms |
23892 KB |
Output is correct |
14 |
Correct |
13 ms |
23980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
33516 KB |
Output is correct |
2 |
Correct |
113 ms |
33596 KB |
Output is correct |
3 |
Correct |
68 ms |
28424 KB |
Output is correct |
4 |
Correct |
27 ms |
27260 KB |
Output is correct |
5 |
Correct |
94 ms |
36820 KB |
Output is correct |
6 |
Correct |
102 ms |
36872 KB |
Output is correct |
7 |
Correct |
69 ms |
32068 KB |
Output is correct |
8 |
Correct |
109 ms |
41772 KB |
Output is correct |
9 |
Correct |
113 ms |
39688 KB |
Output is correct |
10 |
Correct |
65 ms |
31560 KB |
Output is correct |
11 |
Correct |
88 ms |
34892 KB |
Output is correct |
12 |
Correct |
50 ms |
30108 KB |
Output is correct |
13 |
Correct |
59 ms |
29652 KB |
Output is correct |
14 |
Correct |
108 ms |
41812 KB |
Output is correct |
15 |
Correct |
110 ms |
41832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23892 KB |
Output is correct |
2 |
Correct |
12 ms |
23960 KB |
Output is correct |
3 |
Correct |
12 ms |
23812 KB |
Output is correct |
4 |
Correct |
12 ms |
23920 KB |
Output is correct |
5 |
Correct |
12 ms |
23892 KB |
Output is correct |
6 |
Correct |
12 ms |
23964 KB |
Output is correct |
7 |
Correct |
12 ms |
23836 KB |
Output is correct |
8 |
Correct |
12 ms |
23892 KB |
Output is correct |
9 |
Correct |
12 ms |
23892 KB |
Output is correct |
10 |
Correct |
12 ms |
23840 KB |
Output is correct |
11 |
Correct |
12 ms |
23932 KB |
Output is correct |
12 |
Correct |
12 ms |
23792 KB |
Output is correct |
13 |
Correct |
12 ms |
23892 KB |
Output is correct |
14 |
Correct |
13 ms |
23980 KB |
Output is correct |
15 |
Correct |
113 ms |
33516 KB |
Output is correct |
16 |
Correct |
113 ms |
33596 KB |
Output is correct |
17 |
Correct |
68 ms |
28424 KB |
Output is correct |
18 |
Correct |
27 ms |
27260 KB |
Output is correct |
19 |
Correct |
94 ms |
36820 KB |
Output is correct |
20 |
Correct |
102 ms |
36872 KB |
Output is correct |
21 |
Correct |
69 ms |
32068 KB |
Output is correct |
22 |
Correct |
109 ms |
41772 KB |
Output is correct |
23 |
Correct |
113 ms |
39688 KB |
Output is correct |
24 |
Correct |
65 ms |
31560 KB |
Output is correct |
25 |
Correct |
88 ms |
34892 KB |
Output is correct |
26 |
Correct |
50 ms |
30108 KB |
Output is correct |
27 |
Correct |
59 ms |
29652 KB |
Output is correct |
28 |
Correct |
108 ms |
41812 KB |
Output is correct |
29 |
Correct |
110 ms |
41832 KB |
Output is correct |
30 |
Correct |
110 ms |
38404 KB |
Output is correct |
31 |
Correct |
130 ms |
38348 KB |
Output is correct |
32 |
Correct |
59 ms |
31064 KB |
Output is correct |
33 |
Correct |
27 ms |
27220 KB |
Output is correct |
34 |
Correct |
96 ms |
36812 KB |
Output is correct |
35 |
Correct |
105 ms |
36856 KB |
Output is correct |
36 |
Correct |
70 ms |
32096 KB |
Output is correct |
37 |
Correct |
108 ms |
41772 KB |
Output is correct |
38 |
Correct |
143 ms |
40644 KB |
Output is correct |
39 |
Correct |
64 ms |
31668 KB |
Output is correct |
40 |
Correct |
89 ms |
34912 KB |
Output is correct |
41 |
Correct |
51 ms |
30156 KB |
Output is correct |
42 |
Correct |
60 ms |
29636 KB |
Output is correct |
43 |
Correct |
111 ms |
41748 KB |
Output is correct |
44 |
Correct |
109 ms |
41760 KB |
Output is correct |
45 |
Correct |
112 ms |
41768 KB |
Output is correct |
46 |
Correct |
75 ms |
35552 KB |
Output is correct |
47 |
Correct |
85 ms |
35032 KB |
Output is correct |
48 |
Correct |
95 ms |
35532 KB |
Output is correct |
49 |
Correct |
89 ms |
35480 KB |
Output is correct |
50 |
Correct |
10 ms |
23836 KB |
Output is correct |