QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#537330 | #6127. Kawa Exam | Pepsee | ML | 0ms | 13852kb | C++14 | 4.4kb | 2024-08-30 10:07:32 | 2024-08-30 10:07:32 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
inline int read(){
int s=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){s=(s<<1)+(s<<3)+(c^48);c=getchar();}
return s*f;
}
void write(int x){
if(x<0){putchar('-');x=-x;}
if(x>9)write(x/10);
putchar(x%10+'0');
}
int t,n,m,Ans[100005];
int head[100005],H[100005],tot,cnt;
struct node{int nex,to;} edge[100005<<1],E[100005<<1];
int a[100005],tmp[100005],Siz;
int U[100005],V[100005];
vector<int> val[100005];
int mp[100005],Cmp[100005],Cnt[100005],CCnt[100005];
int dfn[100005],low[100005],belong[100005],stk[100005],top,ecc;
inline void add(int x,int y){edge[++tot]={head[x],y};head[x]=tot;}
inline void edge_add(int x,int y){E[++cnt]={H[x],y};H[x]=cnt;}
namespace elia
{
int fa[100005],son[100005],siz[100005],dep[100005];
int rk[100005],in[100005],out[100005];
pair<int,int> sum,Csum;
void dfs(int x,int Fa)
{
in[x] = ++tot; rk[tot] = x;
fa[x] = Fa; siz[x] = 1; dep[x] = dep[Fa]+1;
for (int i = H[x]; i; i = E[i].nex)
{
int v = E[i].to;
if (v == Fa) continue;
dfs(v,x);
siz[x] += siz[v];
if (siz[v] > siz[son[x]])
son[x] = v;
}
out[x] = tot;
}
void Upd(int x,int up)
{
Cnt[mp[x]]--; mp[x] += up; Cnt[mp[x]]++;
if (up==1 && sum.first<mp[x])
sum = make_pair(mp[x],x);
if (up==-1 && sum.first==mp[x]+1 && !Cnt[mp[x]+1])
sum = make_pair(mp[x],x);
}
void Mod(int x,int up)
{
CCnt[Cmp[x]]--; Cmp[x] += up; CCnt[Cmp[x]]++;
if (up==1 && Csum.first<Cmp[x])
Csum = make_pair(Cmp[x],x);
if (up==-1 && Csum.first==Cmp[x]+1 && !CCnt[Cmp[x]+1])
Csum = make_pair(Cmp[x],x);
}
inline void calc(int x)
{
for (int i = H[x]; i; i = E[i].nex)
if (E[i].to != fa[x] && E[i].to != son[x])
for (int j = in[E[i].to]; j <= out[E[i].to]; j++)
for (int k = 0; k < val[rk[j]].size(); k++)
Upd(val[rk[j]][k],1),Mod(val[rk[j]][k],-1);
}
void Solve(int x,int opt)
{
for (int i = H[x]; i; i = E[i].nex)
if (E[i].to != fa[x] && E[i].to != son[x])
Solve(E[i].to,0);
if (son[x]) Solve(son[x],1);
calc(x);
for (int i = 0; i < val[x].size(); i++)
Upd(val[x][i],1),Mod(val[x][i],-1);
Ans[x] = Csum.first+sum.first;
if (!opt)
{
for (int i = in[x]; i <= out[x]; i++)
for (int j = 0; j < val[rk[i]].size(); j++)
Upd(val[rk[i]][j],-1),Mod(val[rk[i]][j],1);
}
}
}
using namespace elia;
void init()
{
for (int i = 1; i <= n+1; i++) tmp[i] = dfn[i] = low[i] = belong[i] = head[i] = H[i] = 0,val[i].clear();
for (int i = 1; i <= Siz; i++) Cnt[i] = CCnt[i] = Cmp[i] = mp[i] = 0;
for (int i = 1; i <= m; i++) edge[i] = E[i] = (node){0,0};
while (top) stk[top--] = 0;
sum = Csum = make_pair(0,0); Siz = cnt = ecc = 0;
tot = 1;
}
void tarjan(int x,int pre)
{
low[x] = dfn[x] = ++tot;
stk[++top] = x;
for (int i = head[x]; i; i = edge[i].nex)
{
int v = edge[i].to;
if (!dfn[v])
{
tarjan(v,i);
low[x] = min(low[x],low[v]);
}
else if (i != (pre^1))
low[x] = min(low[x],dfn[v]);
}
if (dfn[x] == low[x])
{
belong[x] = ++ecc;
val[ecc].push_back(a[x]);
while (stk[top] != x) val[ecc].push_back(a[stk[top]]),belong[stk[top--]] = ecc;
top--;
}
}
void solve()
{
init();
n = read(); m = read();
for (int i = 1; i <= n; i++) a[i] = read(),tmp[i] = a[i];
sort(tmp+1,tmp+n+1);
Siz = unique(tmp+1,tmp+n+1)-tmp-1;
for (int i = 1; i <= n; i++)
{
a[i] = lower_bound(tmp+1,tmp+Siz+1,a[i])-tmp;
CCnt[Cmp[a[i]]]--; Cmp[a[i]]++; CCnt[Cmp[a[i]]]++;
if (Cmp[a[i]] > Csum.first) Csum = make_pair(Cmp[a[i]],a[i]);
}
for (int i = 1; i <= m; i++) U[i] = read(),V[i] = read(),add(U[i],V[i]),add(V[i],U[i]);
tot = 0;
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i,0);
for (int i = 1; i <= m; i++)
if (belong[U[i]] != belong[V[i]])
edge_add(belong[U[i]],belong[V[i]]),edge_add(belong[V[i]],belong[U[i]]);
ecc++;
for (int i = 1; i < ecc; i++)
if (!fa[i]) edge_add(i,ecc),edge_add(ecc,i),dfs(i,ecc);
tot = 0; dfs(ecc,0);
for (int i = 1; i <= m; i++)
if (elia::dep[belong[U[i]]] < elia::dep[belong[V[i]]])
swap(U[i],V[i]);
Cnt[0] = Siz;
Solve(ecc,1);
for (int i = 1; i <= m; i++)
{
if (belong[U[i]] == belong[V[i]]) write(Ans[ecc]);
else write(Ans[belong[U[i]]]);
if (i < m) putchar(' ');
}
puts("");
}
int main()
{
t = read();
while (t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 13852kb
input:
3 7 5 1 2 1 2 1 2 1 1 2 1 3 2 4 5 6 5 7 3 3 1 2 3 1 2 1 3 2 3 2 3 12345 54321 1 2 1 2 1 1
output:
6 5 5 5 4 1 1 1 1 1 1
result:
ok 3 lines
Test #2:
score: -100
Memory Limit Exceeded
input:
5557 2 7 79960 79960 2 2 1 1 1 1 2 2 1 1 2 1 1 2 9 8 21881 70740 70740 21881 22458 22458 639 21881 70740 3 3 1 6 5 8 7 5 5 7 2 3 5 1 7 6 6 7 13064 20716 6746 13064 6746 69225 5 5 4 1 4 1 1 6 4 5 3 2 3 2 8 4 45146 14400 45146 45146 14400 72969 14400 45146 8 6 1 3 4 6 8 3 18 13 48132 37949 92338 92338...