Submission #240626

#TimeUsernameProblemLanguageResultExecution timeMemory
240626mhy908Rigged Roads (NOI19_riggedroads)C++14
19 / 100
443 ms74808 KiB
#include <bits/stdc++.h> #define eb emplace_back #define mp make_pair #define F first #define S second #define all(x) x.begin(), x.end() #define svec(x) sort(x.begin(), x.end()) #define press(x) x.erase(unique(x.begin(), x.end()), x.end()) #define lb(x, v) lower_bound(x.begin(), x.end(), v) #define ub(x, v) upper_bound(x.begin(), x.end(), v) using namespace std; typedef long long LL; typedef pair<int, int> pii; typedef pair<LL, LL> pll; typedef pair<int, LL> pil; typedef pair<LL, int> pli; const LL llinf=2000000000000000000; const LL mod1=1000000007; const LL mod2=998244353; const int inf=2000000000; int n, m, ans[300010]; pii edge[300010]; bool ch[300010]; vector<int> link[300010]; int d[300010], sp[300010][30], num, p[300010], cnt[300010]; void dfs(int num, int par){ d[num]=d[par]+1, sp[num][0]=par, p[num]=num; for(auto i:link[num]){ if(i!=par)dfs(i, num); } } int lca(int x, int y){ if(d[x]>d[y])swap(x, y); for(int i=19; i>=0; i--){ if(d[y]-d[x]>=(1<<i))y=sp[y][i]; } if(x==y)return x; for(int i=19; i>=0; i--){ if(sp[x][i]!=sp[y][i]){ x=sp[x][i]; y=sp[y][i]; } } return sp[x][0]; } int main(){ scanf("%d %d", &n, &m); for(int i=1; i<=m; i++)scanf("%d %d", &edge[i].F, &edge[i].S); for(int i=1; i<n; i++){ int a; scanf("%d", &a); ch[a]=true; link[edge[a].F].eb(edge[a].S); link[edge[a].S].eb(edge[a].F); } dfs(1, 0); for(int j=1; j<20; j++){ for(int i=1; i<=n; i++)sp[i][j]=sp[sp[i][j-1]][j-1]; } for(int i=1; i<=m; i++){ if(d[edge[i].F]<d[edge[i].S])swap(edge[i].F, edge[i].S); if(ch[i])cnt[edge[i].F]=i; } for(int i=1; i<=m; i++){ if(ans[i])continue; if(ch[i]){ ans[i]=++num; p[edge[i].F]=p[edge[i].S]; continue; } int l=lca(edge[i].F, edge[i].S); int a=p[edge[i].F], b=p[edge[i].S]; vector<int> vc; while(d[a]>d[l]){vc.eb(cnt[a]); p[a]=p[l]; a=p[sp[a][0]];} while(d[b]>d[l]){vc.eb(cnt[b]); p[b]=p[l]; b=p[sp[b][0]];} svec(vc); for(auto j:vc)ans[j]=++num; ans[i]=++num; } for(int i=1; i<=m; i++)printf("%d ", ans[i]); }

Compilation message (stderr)

riggedroads.cpp: In function 'int main()':
riggedroads.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~~
riggedroads.cpp:48:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i=1; i<=m; i++)scanf("%d %d", &edge[i].F, &edge[i].S);
                            ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
riggedroads.cpp:51:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &a);
         ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...