QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#404559 | #2543. Edges, Colors and MST | Zxc200611 | WA | 3ms | 13948kb | C++14 | 2.3kb | 2024-05-04 08:25:53 | 2024-05-04 08:25:55 |
Judging History
answer
/*
对于一条非树边,它的权值要比跨过的树边都要大。
要求的是字典序最小的权值排列。
设当前填到权值 v。
若当前未填的,编号最小的边为树边,则直接填上 v+1。
否则其为非树边。把中间的树边按编号从小到大填上 v+1...v+l,然后把 v+l+1 填在这条边上。
又是树剖?树上并查集就好?
*/
#include<bits/stdc++.h>
using namespace std;
const int mxlg=20;
struct DisjointSetUnion
{
int fth[210000];
void init(int n)
{
iota(fth,fth+n+1,0);
}
int find(int u)
{
return fth[u]==u?u:(fth[u]=find(fth[u]));
}
void merge(int u,int v)
{
fth[find(u)]=find(v);
}
};
struct Edge
{
int u,v,ty,id;
};
int n,m;
Edge edg[210000];
vector<Edge> t[210000];
int feid[210000],dep[210000],fth[210000][22];
DisjointSetUnion dsu;
int ans[210000];
void dfs(int u,int f)
{
cout<<"dfs u="<<u<<" f="<<f<<endl;
fth[u][0]=f,dep[u]=dep[f]+1;
for(int i=1;i<=mxlg;i++)
fth[u][i]=fth[fth[u][i-1]][i-1];
for(int i=0;i<t[u].size();i++)
{
int v=t[u][i].v;
if(v==f)
continue;
feid[v]=t[u][i].id;
dfs(v,u);
}
}
int lca(int u,int v)
{
cout<<"lca "<<u<<" "<<v<<" = ";
if(dep[u]<dep[v])
swap(u,v);
for(int i=mxlg;i>=0;i--)
{
if(dep[fth[u][i]]>=dep[v])
u=fth[u][i];
}
for(int i=mxlg;i>=0;i--)
{
if(fth[u][i]!=fth[v][i])
u=fth[u][i],v=fth[v][i];
}
cout<<(u==v?u:fth[u][0])<<endl;
return u==v?u:fth[u][0];
}
void getEdges(int u,int f,vector<int> &res)
{
u=dsu.find(u);
while(dep[u]>dep[f])
{
res.push_back(feid[u]);
dsu.merge(u,fth[u][0]);
u=dsu.find(u);
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
for(int i=1;i<=m;i++)
{
int u,v,ty;
cin>>u>>v>>ty;
edg[i]=((Edge){u,v,ty,i});
if(ty==1)
{
t[u].push_back((Edge){u,v,ty,i});
t[v].push_back((Edge){v,u,ty,i});
}
}
dfs(1,0);
dsu.init(n);
for(int i=1,c=0;i<=m;i++)
{
// cout<<"Edge #"<<i<<" ("<<edg[i].u<<","<<edg[i].v<<") ty="<<edg[i].ty<<endl;
vector<int> eid(0);
int l=lca(edg[i].u,edg[i].v);
getEdges(edg[i].u,l,eid),getEdges(edg[i].v,l,eid);
sort(eid.begin(),eid.end());
for(int x:eid)
ans[x]=++c;
if(edg[i].ty==0)
ans[edg[i].id]=++c;
}
for(int i=1;i<=m;i++)
cout<<ans[i]<<" ";
cout<<endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 3ms
memory: 13948kb
input:
4 5 1 2 0 2 3 1 3 4 1 2 4 0 1 3 1
output:
dfs u=1 f=0 dfs u=3 f=1 dfs u=2 f=3 dfs u=4 f=3 lca 1 2 = 1 lca 2 3 = 3 lca 3 4 = 3 lca 2 4 = 3 lca 1 3 = 1 3 1 4 5 2
result:
wrong output format Expected integer, but "dfs" found