Submission #1269975

#TimeUsernameProblemLanguageResultExecution timeMemory
1269975herominhsteveRigged Roads (NOI19_riggedroads)C++20
100 / 100
422 ms59028 KiB
#include <bits/stdc++.h> #define el '\n' #define FNAME "NOI19_riggedroads" #define allof(x) x.begin(),x.end() #define allof1(x) x.begin()+1,x.end() #define mset(x,n) memset(x,(n),sizeof(x)) using namespace std; const long long MOD = (long long) 1e9+7; template<class X,class Y> bool minimize(X &a,Y b){ if (a>b) {a=b; return true;} return false;} template<class X,class Y> bool maximize(X &a,Y b){ if (a<b) {a=b; return true;} return false;} void setup(){ ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); if (fopen(FNAME".inp","r")){ freopen(FNAME".inp","r",stdin); freopen(FNAME".out","w",stdout); } } const int MAXN = 3e5 + 5; int n,m; vector<int> graph[MAXN]; bool inMST[MAXN]; inline pair<int,int> uv(int u,int v){ return make_pair(min(u,v),max(u,v)); } map<pair<int,int>,int> eID; vector<pair<int,int>> edges; void init(){ cin>>n>>m; for (int i=0;i<m;i++){ int u,v; cin>>u>>v; edges.emplace_back(u,v); eID[uv(u,v)] = i; } for (int i=1;i<n;i++){ int e; cin>>e; e--; inMST[e]=1; int u,v; tie(u,v) = edges[e]; graph[u].push_back(v); graph[v].push_back(u); } } int depth[MAXN],parent[MAXN]; bool visited[MAXN]; void dfs(int u,int pre){ visited[u] = 1; parent[u] = pre; for (int v : graph[u]){ if (!visited[v]){ depth[v] = depth[u] + 1; dfs(v,u); } } } namespace DSU{ vector<int> parent,power,rep; void make_set(){ parent.resize(n+1,0); power.resize(n+1,1); rep.assign(n+1,0); for (int i=1;i<=n;i++){ parent[i]=i; power[i]=1; rep[i] = i; } } int find(int u){ if (u==parent[u]) return u; return parent[u]=find(parent[u]); } bool Union(int u,int v){ u=find(u); v=find(v); if (u==v) return false; if (power[u]<power[v]) swap(u,v); parent[v]=u; power[u]+=power[v]; if (depth[rep[u]] > depth[rep[v]]) rep[u] = rep[v]; return true; } int getRep(int u){ return rep[find(u)]; } }; void sol(){ dfs(1,1); DSU::make_set(); int cur=0; vector<int> res(m,0); for (int i=0;i<m;i++){ int u,v; tie(u,v) = edges[i]; if (inMST[i]){ if (!res[i]){ res[i] = ++cur; DSU::Union(u,v); } } else{ u = DSU::getRep(u); v = DSU::getRep(v); vector<int> indices; while (u ^ v){ if (depth[u] < depth[v]) swap(u,v); indices.push_back(eID[uv(u,parent[u])]); DSU::Union(u,parent[u]); u = DSU::getRep(u); } sort(allof(indices)); for (int x : indices) res[x]=++cur; res[i] = ++cur; } } for (int x : res) cout<<x<<" "; } int main(){ setup(); init(); sol(); }

Compilation message (stderr)

riggedroads.cpp: In function 'void setup()':
riggedroads.cpp:16:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |                 freopen(FNAME".inp","r",stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
riggedroads.cpp:17:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |                 freopen(FNAME".out","w",stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#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...