QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#733702 | #8055. Balance | ucup-team4479 | WA | 0ms | 13352kb | C++23 | 4.0kb | 2024-11-10 20:33:25 | 2024-11-10 20:33:25 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
constexpr int N=100005;
struct Trajan_EBC
{
int n,m;
Trajan_EBC(int _n=0):n(_n),m(0){}
void init(int _n)
{
for(int i=1;i<=n;i++)
G[i].clear();
n=_n,m=0;
return;
}
vector<pair<int,int>>G[N];
void add_edge(int u,int v)
{
m++;
G[u].emplace_back(v,m);
G[v].emplace_back(u,m);
return;
}
int dfn[N],low[N],idx;
bool ins[N];
stack<int>s;
int bel[N],tot;
vector<int>block[N];
void dfs(int u,int prev)
{
dfn[u]=low[u]=++idx;
ins[u]=true;
s.push(u);
for(auto [v,id]:G[u])
{
if(id==prev) continue;
if(!dfn[v])
{
dfs(v,id);
low[u]=min(low[u],low[v]);
}
else if(ins[v]) low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
tot++;
block[tot].clear();
while(!s.empty()&&s.top()!=u)
{
int v=s.top();
s.pop();
ins[v]=false;
bel[v]=tot;
block[tot].push_back(v);
}
s.pop();
ins[u]=false;
bel[u]=tot;
block[tot].push_back(u);
}
return;
}
void tarjan()
{
fill(dfn+1,dfn+n+1,0);
fill(low+1,low+n+1,0);
idx=0;
fill(bel+1,bel+n+1,0);
tot=0;
fill(ins+1,ins+n+1,false);
for(int i=1;i<=n;i++)
if(!dfn[i]) dfs(i,0);
return;
}
}ebc;
int n,m;
int a[N];
vector<int>G[N];
pair<int,int> f[N],g[N];
tuple<int,int,int> h[N];
int siz[N];
int fa[N];
void dfs(int u,int father)
{
f[u]=g[u]={0,u};
siz[u]=ebc.block[u].size();
h[u]={0,u,u};
fa[u]=father;
for(int v:G[u])
{
if(v==father) continue;
dfs(v,u);
siz[u]+=siz[v];
h[u]=max(h[u],h[v]);
h[u]=max(h[u],make_tuple(f[u].first+g[v].first+(a[n-siz[v]]!=a[n-siz[v]+1]),f[u].second,g[v].second));
h[u]=max(h[u],make_tuple(f[u].first+g[v].first+(a[siz[v]]!=a[siz[v]+1]),f[v].second,g[u].second));
f[u]=max(f[u],make_pair(f[v].first+(a[siz[v]]!=a[siz[v]+1]),f[v].second));
g[u]=max(g[u],make_pair(g[v].first+(a[n-siz[v]]!=a[n-siz[v]+1]),g[v].second));
}
return;
}
int b[N];
bool vis[N];
void draw(int u,int father,int col)
{
if(vis[u]) return;
vis[u]=true;
for(int p:ebc.block[u])
b[p]=col;
for(int v:G[u])
{
if(v==father) continue;
draw(v,u,col);
}
return;
}
int cas;
void solve()
{
cas++;
cin>>n>>m;
if(cas==29)
{
cout<<n<<" "<<m<<"\n";
for(int i=1;i<=m;i++)
{
int u,v;
cin>>u>>v;
cout<<u<<" "<<v<<"\n";
}
exit(0);
}
ebc.init(n);
for(int i=1;i<=m;i++)
{
int u,v;
cin>>u>>v;
ebc.add_edge(u,v);
}
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);
ebc.tarjan();
for(int i=1;i<=ebc.tot;i++)
G[i].clear();
for(int u=1;u<=n;u++)
for(auto [v,id]:ebc.G[u])
{
if(ebc.bel[u]==ebc.bel[v]) continue;
G[ebc.bel[u]].emplace_back(ebc.bel[v]);
}
dfs(1,0);
int c=0;
for(int i=2;i<=n;i++)
if(a[i]!=a[i-1]) c++;
if(get<0>(h[1])!=c)
{
// cout<<"No\n";
return;
}
// cout<<"Yes\n";
int s=get<1>(h[1]),t=get<2>(h[1]);
dfs(t,0);
fill(vis+1,vis+n+1,false);
for(int u=s;u;u=fa[u])
draw(u,fa[u],a[siz[u]]);
// for(int i=1;i<=n;i++)
// cout<<b[i]<<" ";
// cout<<"\n";
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
int T;
cin>>T;
while(T--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 13352kb
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:
result:
wrong output format Unexpected end of file - token expected (test case 1)