QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#477511 | #2214. Link Cut Digraph | 3mara | WA | 301ms | 107556kb | C++14 | 2.1kb | 2024-07-14 04:34:24 | 2024-07-14 04:34:26 |
Judging History
answer
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define F first
#define S second
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N=3e5+1, MOD=1e9+7;
vector<int> adj[N];
int n,q,vis[N],par[N];
vector<int> v;
void dfs(int u){
if(vis[u]) return;
vis[u]=1;
for(auto i:adj[u]){
if(i<0) continue;
dfs(i);
}
v.push_back(u);
}
void dfs2(int u, int curp){
for(auto i:adj[u]){
if(i>0) continue;
if(vis[-i]) continue;
vis[-i]=1;
par[-i]=curp;
dfs2(-i, curp);
}
v.push_back(u);
}
int ans=0;
struct dsu{
int par[N],sz[N];
void build(int n){
for(int i=1;i<=n;i++) par[i]=i,sz[i]=1;
}
int get(int u){
return par[u]=(par[u]==u?u:get(par[u]));
}
void add(int a, int b){
a=get(a); b=get(b);
if(a==b) return;
ans+=sz[a]*sz[b];
if(sz[a]<sz[b]) swap(a,b);
sz[a]+=sz[b]; par[b]=a;
}
} AAA;
void hi(int l, int r, vector<pair<int,pair<int,int>>> edges){
if(l==q) return;
if(l==r){
for(auto i:edges){
AAA.add(i.S.F,i.S.S);
}
cout<<ans<<'\n';
return;
}
int m=(l+r)/2;
for(auto i:edges){
adj[i.S.F].clear(); adj[i.S.S].clear();
vis[i.S.F]=vis[i.S.S]=0;
}
for(auto i:edges){
if(i.F>m) continue;
adj[i.S.F].push_back(i.S.S);
adj[i.S.S].push_back(-i.S.F);
}
for(auto i:edges){
if(vis[i.S.F]) continue;
dfs(i.S.F);
}
for(auto i:edges){
vis[i.S.F]=vis[i.S.S]=0;
}
while(v.size()){
int u=v.back(); v.pop_back();
if(!vis[u]) par[u]=u,vis[u]=1,dfs2(u,u);
}
vector<pair<int,pair<int,int>>> ledges,redges;
for(auto i:edges){
if(par[i.S.F]==par[i.S.S]) ledges.push_back(i);
else redges.push_back({i.F,{par[i.S.F],par[i.S.S]}});
}
hi(l,m,ledges); hi(m+1,r,redges);
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>q; AAA.build(n);
vector<pair<int,pair<int,int>>> edges;
for(int i=0;i<q;i++){
int x,y; cin>>x>>y;
edges.push_back({i,{x,y}});
}
hi(0,q,edges);
}
Details
Test #1:
score: 0
Wrong Answer
time: 301ms
memory: 107556kb