QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#23012 | #2543. Edges, Colors and MST | foreverlasting# | WA | 3ms | 5796kb | C++20 | 2.7kb | 2022-03-11 15:22:36 | 2022-04-30 00:39:46 |
Judging History
answer
#include<bits/stdc++.h>
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],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,tg;
}tr[mxn<<2];
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) {tr[x].v=0; tr[x].tg=1;}
void pushdown(int x)
{
if(tr[x].tg)
{
chg(x<<1);chg(x<<1|1);
tr[x].tg=0;
}
}
void build(int x,int l,int r)
{
tr[x].v=r-l+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)
{
if(L<=l&&r<=R) return chg(x);
pushdown(x);
if(L<=mid) update(x<<1,l,mid,L,R);
if(R>mid) update(x<<1|1,mid+1,r,L,R);
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;
}
#undef mid
void upd(int u,int v)
{
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]);
}
if(dep[u]>dep[v]) swap(u,v);
if(u^v) update(1,1,n,dfn[u]+1,dfn[v]);
}
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 s1=0,s2=0;
for(int x,i=1;i<=m;++i)
if(a[i].z)
{
x=max(dfn[a[i].x],dfn[a[i].y]);
if(!query(1,1,n,x,x))
{
for(++s1;ex[s1];++s1);
ex[ans[i]=s1]=1;
}
else ex[ans[i]=++s2]=1,update(1,1,n,x,x);
}
else
{
x=qry(a[i].x,a[i].y);upd(a[i].x,a[i].y);
ex[ans[i]=(s2+=x+1)]=1;
}
for(int i=1;i<=m;++i) printf("%d%c",ans[i]," \n"[i==m]);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5796kb
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: -100
Wrong Answer
time: 3ms
memory: 5792kb
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 3 4 7 9 11 15
result:
wrong answer 10th numbers differ - expected: '9', found: '3'