QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#139779 | #6127. Kawa Exam | sihan_88 | TL | 1ms | 13400kb | C++14 | 2.7kb | 2023-08-14 16:11:01 | 2023-08-14 16:11:02 |
Judging History
answer
#include<cstdio>
#include<map>
#include<vector>
char buf[1<<21],*p1=buf,*p2=buf;
inline char Getc(){if(p1==p2){p2=(p1=buf)+fread(buf,1,1<<21,stdin);if(p1==p2) return EOF;}return *p1++;}
inline int rd()
{
static char ch;
while(ch=Getc(),ch<'0'||ch>'9');
int x=ch^'0';
while(ch=Getc(),'0'<=ch&&ch<='9') x=(x<<1)+(x<<3)+(ch^'0');
return x;
}
const int N=100010;
int head[N],to[N<<1],nxt[N<<1],p;
inline void add(int u,int v){++p;to[p]=v,nxt[p]=head[u];head[u]=p;}
int dfn[N],low[N],cl;
bool cut[N];
int ans[N];
void dfs(int u,int e)
{
dfn[u]=low[u]=++cl;
for(int i=head[u];i;i=nxt[i])
{
if(i==(e^1)) continue;
int v=to[i];
if(!dfn[v]) dfs(v,i),low[u]=std::min(low[u],low[v]);
else low[u]=std::min(low[u],dfn[v]);
cut[i>>1]=(low[v]>dfn[u]);
}
}
bool vis[N];
std::map<int,int> mp;
int a[N];
int seq[N],sz[N],hson[N];
std::vector<std::pair<int,int> > G[N];
void dfs2(int u)
{
vis[u]=true,++mp[a[u]];
sz[u]=1,dfn[u]=++cl,seq[cl]=u;
for(int i=head[u];i;i=nxt[i])
{
int v=to[i];
if(!vis[v])
{
G[u].emplace_back(std::make_pair(v,i)),dfs2(v),sz[u]+=sz[v];
if(sz[v]>sz[hson[u]]) hson[u]=v;
}
}
}
int loc[N],cnt[N],mx,tot;
struct Node{int x,y;}t[N<<2];
int pos[N];
inline void pushup(int o){t[o].x=std::max(t[o<<1].x,t[(o<<1)|1].x),t[o].y=std::max(t[o<<1].y,t[(o<<1)|1].y);}
void build(int o,int ls,int rs)
{
if(ls==rs){t[o].x=0,t[o].y=cnt[ls],pos[ls]=o;return ;}
int mid=(ls+rs)>>1;
build(o<<1,ls,mid),build((o<<1)|1,mid+1,rs);
pushup(o);
}
inline void Modify(int A,int B)
{
int o=pos[A];
t[o].x+=B,t[o].y-=B;
for(o>>=1;o;o>>=1) pushup(o);
}
void dfs3(int u,int e,bool k)
{
for(auto &x:G[u])
{
int v=x.first;
if(v!=hson[u]) dfs3(v,x.second,false);
}
for(auto &x:G[u])
{
int v=x.first;
if(v==hson[u]) dfs3(v,x.second,true);
}
for(auto &x:G[u])
{
int v=x.first;
if(v!=hson[u])
for(int j=dfn[v];j<dfn[v]+sz[v];++j)
Modify(loc[a[seq[j]]],1);
}
Modify(loc[a[u]],1);
if(cut[e>>1]) ans[e>>1]=t[1].x+t[1].y-mx;
if(!k) for(int i=dfn[u];i<dfn[u]+sz[u];++i) Modify(loc[a[seq[i]]],-1);
}
int main()
{
int T=rd(),n,m;
t[0].x=t[0].y=0;
while(T--)
{
n=rd(),m=rd();
for(int i=1;i<=n;++i) a[i]=rd(),head[i]=dfn[i]=0,vis[i]=false,G[i].clear();
p=1,cl=0;
for(int i=1;i<=m;++i)
{
int u=rd(),v=rd();
add(u,v),add(v,u),ans[i]=0;
}
int sum=0;
for(int i=1;i<=n;++i)
{
if(dfn[i]) continue;
dfs(i,0);
cl=0,mp.clear(),dfs2(i);
mx=tot=0;
for(auto &x:mp) loc[x.first]=++tot,cnt[tot]=x.second,mx=std::max(mx,x.second);
sum+=mx,build(1,1,tot),dfs3(i,0,false);
}
for(int i=1;i<m;++i) printf("%d ",ans[i]+sum);
printf("%d\n",ans[m]+sum);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 13400kb
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
Time 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...
output:
2 2 2 2 2 2 2 6 6 7 6 6 6 6 6 3 3 3 4 4 3 3 7 7 7 7 9 9 9 8 9 8 9 8 9 9 10 9 9 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 7 8 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 9 10 9 16 16 16 16 16 17 16 16 10 10 11 10 12 11 10 10 10 10 10 10 10 12 10 10 10 10 10 11 10 9 9 9 9 9 9 9 9 9 9 9 9 9 10 ...