QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#23020 | #2543. Edges, Colors and MST | foreverlasting# | WA | 3ms | 24280kb | C++20 | 3.2kb | 2022-03-11 15:54:26 | 2022-04-30 02:13:34 |
Judging History
answer
#include<bits/stdc++.h>
#define pii pair<int,int>
#define fr first
#define sc second
#define mp make_pair
using namespace std;
#define RG
#define LL long long
#define gc getchar
#define pb push_back
const int mxn=1e6+10;
int n,m,ex[mxn],pos[mxn],ans[mxn];
int tim,fa[mxn],dep[mxn],siz[mxn],son[mxn],dfn[mxn],top[mxn];
int t,h[mxn];
struct Tre
{
int to,nxt;
}e[mxn<<1];
struct Edge
{
int x,y,z;
}a[mxn];
struct Seg_tree
{
int v,mn,tg;
}tr[mxn<<2];
pii c[mxn];
inline int rd(){
int s=0,w=1,ch=gc();
while(ch<'0'||ch>'9'){
if(ch=='-')w=-1;
ch=gc();
}
while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
return s*w;
}
void add(int u,int v)
{
e[++t]=(Tre){v,h[u]};h[u]=t;
e[++t]=(Tre){u,h[v]};h[v]=t;
}
void dfs1(int u)
{
int v;siz[u]=1;
for(int i=h[u];i;i=e[i].nxt)
if((v=e[i].to)!=fa[u])
{
fa[v]=u;dep[v]=dep[u]+1;
dfs1(v);siz[u]+=siz[v];
if(siz[v]>siz[son[u]]) son[u]=v;
}
}
void dfs2(int u,int tp)
{
top[u]=tp;dfn[u]=++tim;
if(son[u]) dfs2(son[u],tp);
int v;
for(int i=h[u];i;i=e[i].nxt)
if((v=e[i].to)!=fa[u]&&v!=son[u])
dfs2(v,v);
}
#define mid ((l+r)>>1)
void chg(int x,int v) {tr[x].v=0; tr[x].mn=min(tr[x].mn,v); tr[x].tg=v;}
void pushdown(int x)
{
if(tr[x].tg)
{
chg(x<<1,tr[x].tg);chg(x<<1|1,tr[x].tg);
tr[x].tg=0;
}
}
void build(int x,int l,int r)
{
tr[x].v=r-l+1;tr[x].mn=m+1;
if(l==r) return ;
build(x<<1,l,mid);build(x<<1|1,mid+1,r);
}
void update(int x,int l,int r,int L,int R,int v)
{
if(L<=l&&r<=R) return chg(x,v);
pushdown(x);
if(L<=mid) update(x<<1,l,mid,L,R,v);
if(R>mid) update(x<<1|1,mid+1,r,L,R,v);
tr[x].v=tr[x<<1].v+tr[x<<1|1].v;
}
int query(int x,int l,int r,int L,int R)
{
if(L<=l&&r<=R) return tr[x].v;
int res=0;
pushdown(x);
if(L<=mid) res=query(x<<1,l,mid,L,R);
if(R>mid) res+=query(x<<1|1,mid+1,r,L,R);
return res;
}
void prework(int x,int l,int r)
{
if(l==r) return (void)(c[l]=mp(tr[x].mn,pos[l]));
prework(x<<1,l,mid);prework(x<<1|1,mid+1,r);
}
#undef mid
void upd(int u,int v,int w)
{
for(;top[u]!=top[v];u=fa[top[u]])
{
if(dep[top[u]]<dep[top[v]]) swap(u,v);
update(1,1,n,dfn[top[u]],dfn[u],w);
}
if(dep[u]>dep[v]) swap(u,v);
if(u^v) update(1,1,n,dfn[u]+1,dfn[v],w);
}
int qry(int u,int v)
{
int res=0;
for(;top[u]!=top[v];u=fa[top[u]])
{
if(dep[top[u]]<dep[top[v]]) swap(u,v);
res+=query(1,1,n,dfn[top[u]],dfn[u]);
}
if(dep[u]>dep[v]) swap(u,v);
if(u^v) res+=query(1,1,n,dfn[u]+1,dfn[v]);
return res;
}
int main()
{
n=rd();m=rd();
for(int x,y,z,i=1;i<=m;++i)
{
x=rd();y=rd();z=rd();
a[i]=(Edge){x,y,z};
if(z&1) add(x,y);
}
dfs1(1);dfs2(1,1);build(1,1,n);
int cnt=0;
for(int x,i=1;i<=m;++i)
if(a[i].z)
{
x=max(dfn[a[i].x],dfn[a[i].y]);pos[x]=i;
if(query(1,1,n,x,x)) ex[ans[i]=++cnt]=1,update(1,1,n,x,x,cnt);
}
else
{
x=qry(a[i].x,a[i].y);upd(a[i].x,a[i].y,cnt+=x);
ex[ans[i]=++cnt]=1;
}
prework(1,1,n);
sort(c+2,c+n+1);
for(int nw=0,i=2;i<=n;++i)
if(!ans[c[i].sc])
{
for(++nw;ex[nw];++nw);
ans[c[i].sc]=nw;
}
for(int i=1;i<=m;++i) printf("%d%c",ans[i]," \n"[i==m]);
return 0;
}
/*
4 5
1 2 0
2 3 1
3 4 1
2 4 0
1 3 1
*/
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 24160kb
input:
4 5 1 2 0 2 3 1 3 4 1 2 4 0 1 3 1
output:
3 1 4 5 2
result:
ok 5 number(s): "3 1 4 5 2"
Test #2:
score: 0
Accepted
time: 0ms
memory: 24212kb
input:
9 15 1 4 1 3 5 1 3 9 0 1 3 0 2 5 0 5 8 0 6 9 0 8 9 0 1 7 1 1 8 1 6 8 1 4 9 1 2 4 1 3 4 1 4 6 0
output:
1 2 5 6 8 10 12 13 14 9 11 3 7 4 15
result:
ok 15 numbers
Test #3:
score: 0
Accepted
time: 0ms
memory: 24244kb
input:
8 15 4 7 1 3 6 1 1 5 0 5 7 0 4 5 1 1 2 0 4 8 1 1 3 0 1 4 1 2 4 0 3 8 1 6 8 0 2 5 1 7 8 0 3 5 0
output:
1 2 5 6 3 8 9 11 4 12 10 13 7 14 15
result:
ok 15 numbers
Test #4:
score: 0
Accepted
time: 3ms
memory: 24164kb
input:
6 13 4 5 0 5 6 0 1 2 0 1 6 0 1 5 0 2 3 0 3 5 0 3 6 0 4 6 1 2 4 1 2 5 1 1 3 1 1 4 1
output:
3 5 7 8 9 11 12 13 4 1 2 10 6
result:
ok 13 numbers
Test #5:
score: 0
Accepted
time: 2ms
memory: 24280kb
input:
9 22 2 4 1 5 6 1 3 6 0 1 2 1 1 7 0 2 3 0 7 8 0 3 8 0 6 7 0 5 7 1 8 9 0 1 5 1 3 5 1 2 8 0 1 4 0 1 9 1 1 8 0 5 8 1 2 6 0 3 9 0 5 9 0 2 9 0
output:
1 2 4 5 8 9 11 12 13 6 15 7 3 16 17 14 18 10 19 20 21 22
result:
ok 22 numbers
Test #6:
score: 0
Accepted
time: 2ms
memory: 24256kb
input:
4 6 1 2 1 3 4 0 1 3 1 2 4 0 1 4 1 2 3 0
output:
1 4 2 5 3 6
result:
ok 6 numbers
Test #7:
score: -100
Wrong Answer
time: 2ms
memory: 24244kb
input:
4 6 1 3 0 2 3 0 1 4 0 2 4 1 3 4 1 1 2 1
output:
4 5 6 2 3 1
result:
wrong answer 4th numbers differ - expected: '1', found: '2'