QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#697278 | #8055. Balance | Green_Hand | WA | 37ms | 14052kb | C++20 | 3.5kb | 2024-11-01 12:39:22 | 2024-11-01 12:39:26 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 1E5 + 7;
int n,m,num,T,a[N],b[2][N];
void read(int &x)
{
char c = getchar(); x = 0;
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
}
struct Tree
{
int tot,flag,val[N],siz[N],st[N],col[N],f[N][2],id[N][2],to[N << 1],nxt[N << 1],tag[N << 1];
void init()
{
tot = 1, flag = 0;
for(int i = 1;i <= n; ++ i) st[i] = val[i] = col[i] = 0;
}
void add(int u,int v)
{ to[++tot] = v, nxt[tot] = st[u], st[u] = tot, tag[tot] = 1; }
void add_edge(int u,int v) { add(u,v), add(v,u); }
void dfs(int x,int fa)
{
if(flag) return;
f[x][0] = 0, f[x][1] = num + 1,
id[x][0] = id[x][1] = 0, siz[x] = val[x];
for(int i = st[x],v,t1,t2;i;i = nxt[i])
if((v = to[i]) != fa)
{
dfs(v,x), siz[x] += siz[v];
if(siz[v] == b[0][f[v][0] + 1]) t1 = f[v][0] + 1;
else t1 = f[v][0];
if(t1 + 1 == f[x][1])
{
flag = 1, f[x][0] = t1, id[x][0] = i,
solve(x,0), solve(x,1), col[x] = f[x][1], cover(x);
break;
}
if(siz[v] == b[1][f[v][1] - 1]) t2 = f[v][1] - 1;
else t2 = f[v][1];
if(f[x][0] + 1 == t2)
{
flag = 1, f[x][1] = t2, id[x][1] = i,
solve(x,0), solve(x,1), col[x] = f[x][1], cover(x);
break;
}
if(t1 > f[x][0]) f[x][0] = t1, id[x][0] = i;
if(t2 < f[x][1]) f[x][1] = t2, id[x][1] = i;
}
}
void solve(int x,int type)
{
if(id[x][type])
{
int v = to[id[x][type]];
solve(v,type);
if(f[x][type] != f[v][type])
tag[id[x][type]] = tag[id[x][type] ^ 1] = 0,
col[v] = f[type ? v : x][type], cover(v);
}
}
void cover(int x)
{
for(int i = st[x],v;i;i = nxt[i])
if(tag[i] && !col[v = to[i]]) col[v] = col[x], cover(v);
}
} t;
struct Graph
{
int tot,top,cnt,s[N],st[N],dfn[N],col[N],low[N],to[N << 2],nxt[N << 2];
void init()
{
tot = 1, top = cnt = 0;
for(int i = 1;i <= n; ++ i) st[i] = dfn[i] = 0;
}
void add(int u,int v)
{ to[++tot] = v, nxt[tot] = st[u], st[u] = tot; }
void tarjan(int x,int lst)
{
s[++top] = x, low[x] = dfn[x] = ++cnt;
for(int i = st[x],v;i;i = nxt[i])
if(i != (lst ^ 1))
{
if(!dfn[v = to[i]])
tarjan(v,i), low[x] = min(low[x],low[v]);
else
low[x] = min(low[x],dfn[v]);
}
if(dfn[x] == low[x])
for(s[top + 1] = 0;s[top + 1] != x; --top)
col[s[top]] = x, ++t.val[x];
}
void update()
{
for(int x = 1;x <= n; ++ x)
for(int i = st[x],v;i;i = nxt[i])
if(col[x] < col[v = to[i]]) t.add_edge(col[x],col[v]);
}
} g;
int main()
{
for(read(T);T--;)
{
read(n), read(m), g.init(), t.init(), num = 0;
for(int i = 1,u,v;i <= m; ++ i)
read(u), read(v), g.add(u,v), g.add(v,u);
for(int i = 1;i <= n; ++ i) read(a[i]);
sort(a + 1,a + n + 1);
for(int i = 1;i < n; ++ i)
if(a[i] != a[i + 1]) b[0][++num] = i;
for(int i = 1;i <= num; ++ i)
b[1][i] = b[0][num - i + 1];
b[0][num + 1] = n;
g.tarjan(1,0), g.update(), t.dfs(g.col[1],0);
if(t.val[g.col[1]] == n && !num)
{
printf("Yes\n");
for(int i = 1;i <= n; ++ i)
printf("%d%c",a[i]," \n"[i == n]);
continue;
}
if(!t.flag) { printf("No\n"); continue; }
printf("Yes\n");
for(int i = 1,x;i <= n; ++ i)
x = t.col[g.col[i]],
printf("%d%c",x == num + 1 ? a[n] : a[b[0][x]]," \n"[i == n]);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 12140kb
input:
5 5 4 1 2 2 3 3 4 4 5 1 2 3 4 5 5 4 1 2 1 3 1 4 1 5 1 2 3 4 5 5 4 1 2 1 3 1 4 1 5 1 2 2 2 3 5 6 1 2 1 2 2 3 3 4 4 5 3 5 1 2 1 2 1 2 2 1 2 1 2 1 2
output:
Yes 5 4 3 2 1 No Yes 2 3 1 2 2 Yes 2 2 1 1 1 No
result:
ok ok (5 test cases)
Test #2:
score: -100
Wrong Answer
time: 37ms
memory: 14052kb
input:
100000 4 3 4 2 1 3 2 1 2 1 3 2 5 7 2 5 5 3 2 3 5 1 4 3 2 4 4 3 1 3 1 1 2 3 2 2 1 2 3 1 1 1 4 7 3 4 1 4 2 3 4 3 4 2 3 1 4 3 2 3 3 2 4 6 2 4 3 1 1 2 1 2 2 3 4 2 1 1 3 3 4 7 3 2 4 2 1 2 3 4 3 2 2 4 3 4 2 1 1 1 5 5 3 2 1 5 4 5 4 3 2 1 1 2 2 3 2 3 5 2 3 1 3 3 1 3 1 2 1 3 2 3 2 3 2 1 2 1 1 2 1 1 2 3 2 1 1...
output:
Yes 2 2 3 1 No Yes 1 1 1 No No Yes 2 1 1 1 No No Yes 1 1 Yes 1 1 Yes 1 1 Yes 1 1 1 1 No Yes 1 1 1 1 1 No Yes 1 1 1 Yes 2 1 Yes 1 1 1 1 1 Yes 2 1 No Yes 1 1 Yes 1 1 1 Yes 1 1 Yes 1 1 1 1 Yes 1 1 Yes 2 2 2 2 2 Yes 1 1 1 1 1 Yes 1 1 No No Yes 1 1 No Yes 1 1 No No No Yes 2 1 1 1 1 Yes 2 1 1 1 No No No N...
result:
wrong answer ans finds an answer, but out doesn't (test case 15)