Kawa Exam

题解:先scc把图变成树, 然后对于这若干棵树在进行dsu的操作。

dsu就是先找到最大的子树放在一边,然后先处理小的子树,最后处理大的子树。无限递归。

重要的一点就是 是否重新添加每个点的值,每次处理完小的子树之后会清空影响,然后处理完最大的子树之后就不再清空影响,这样减少复杂度。

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL mod = (int)1e9+;
const int N = 2e5 + ;
int head[N], to[N<<], nt[N<<], tot = ;
void add(int u, int v){
to[tot] = v;
nt[tot] = head[u];
head[u] = tot++;
}
int belong[N], dfn[N], low[N], now_time, scc_cnt;
int now = , st[N], ed[N];
vector<int> vc[N];
vector<pll> e[N];
stack<int> s;
void dfs(int u, int id){
dfn[u] = low[u] = ++now_time;
s.push(u);
for(int i = head[u]; ~i; i = nt[i]){
if(i == (id^)) continue;
if(!dfn[to[i]]) dfs(to[i], i);
if(!belong[to[i]]) low[u] = min(low[u], low[to[i]]);
}
if(dfn[u] == low[u]){
++scc_cnt;
int now;
while(){
now = s.top(); s.pop();
belong[now] = scc_cnt;
vc[scc_cnt].pb(now);
if(now == u) break;
}
}
}
void scc(int n){
for(int i = ; i <= scc_cnt; ++i) {
vc[i].clear();
e[i].clear();
st[i] = ed[i] = ; } now = ;
for(int i = ; i <= n; ++i) dfn[i] = low[i] = belong[i] = ;
while(!s.empty()) s.pop();
now_time = scc_cnt = ;
for(int i = ; i <= n; ++i)
if(!belong[i]) dfs(i,-);
for(int i = , u, v; i <= tot; i += ){
u = to[i], v = to[i+];
u = belong[u], v = belong[v];
if(u != v) e[u].pb(pll(v,i/+)), e[v].pb(pll(u,i/+));
}
}
int vis[N], col[N], col2[N], cnt[N], cnt2[N], mx1, mx2;
int sz[N], p[N];
int a[N];
void Add1(int u){
for(int i = , x; i < vc[u].size(); ++i){
x = vc[u][i];
cnt[col[a[x]]]--;
++col[a[x]];
cnt[col[a[x]]]++;
if(mx1 < col[a[x]]) mx1 = col[a[x]];
}
}
void Add2(int u){
for(int i = , x; i < vc[u].size(); ++i){
x = vc[u][i];
cnt2[col2[a[x]]]--;
++col2[a[x]];
cnt2[col2[a[x]]]++;
if(mx2 < col2[a[x]]) mx2 = col2[a[x]];
}
}
void Sub1(int u){
for(int i = , x; i < vc[u].size(); ++i){
x = vc[u][i];
--cnt[col[a[x]]];
--col[a[x]];
++cnt[col[a[x]]];
if(cnt[mx1] == ) --mx1;
}
}
void Sub2(int u){
for(int i = , x; i < vc[u].size(); ++i){
x = vc[u][i];
--cnt2[col2[a[x]]];
--col2[a[x]];
++cnt2[col2[a[x]]];
if(cnt2[mx2] == ) --mx2;
}
}
void ddfs(int o, int u){
//cout << o << ' ' << u << endl;
++now;
Add1(u);
p[now] = u;
st[u] = now;
sz[u] = vc[u].size();
for(int i = , v; i < e[u].size(); ++i){
v = e[u][i].fi;
if(v == o) continue;
ddfs(u, v);
sz[u] += sz[v];
}
ed[u] = now;
}
void Clear(int o, int u){
Sub1(u);
for(int i = ; i < e[u].size(); ++i){
int v = e[u][i].fi;
if(v == o) continue;
Clear(u, v);
}
}
int ttttmp;
int val[N];
void dsu(int o, int u, int op, int id){
int bigson = -, mx = -, iid;
for(int i = , v; i < e[u].size(); ++i){
v = e[u][i].fi;
if(v == o) continue;
if(mx < sz[v]) {
mx = sz[v];
bigson = v;
iid = e[u][i].se;
}
}
for(int i = , v; i < e[u].size(); ++i){
v = e[u][i].fi;
if(v == o || v == bigson) continue;
dsu(u, v, , e[u][i].se);
}
if(bigson != -)
dsu(u, bigson, , iid);
for(int i = , v; i < e[u].size(); ++i){
v = e[u][i].fi;
if(v != o && v != bigson) {
for(int i = st[v]; i <= ed[v]; i++){
Sub1(p[i]);
Add2(p[i]);
}
}
}
Sub1(u);
Add2(u);
val[id] = mx1+mx2-ttttmp;
if(op == ){
for(int i = st[u]; i <= ed[u]; i++){
Sub2(p[i]);
Add1(p[i]);
}
}
}
void init(){
memset(head, -, sizeof(head));
tot = ;
}
int main(){
int T, n, m;
scanf("%d", &T); while(T--){
init();
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i) scanf("%d", &a[i]);
for(int i = , u, v; i <= m; ++i){
scanf("%d%d", &u, &v);
add(u,v); add(v,u);
}
scc(n);
int ans = ;
for(int i = ; i <= scc_cnt; ++i){
if(st[i]) continue;
mx1 = mx2 = ;
ddfs(,i);
ttttmp = mx1;
ans += mx1;
dsu(,i,,);
Clear(,i);
}
// cout << ans << endl;
for(int i = ; i <= m; i++){
printf("%d%c", ans+val[i]," \n"[i==m]);
val[i] = ;
}
}
return ;
}
/*
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
7 5
1 2 1 2 1 2 1
1 2
1 3
2 4
5 6
5 7
*/

zoj - 4059 Kawa Exam scc + dsu的更多相关文章

  1. 2018 ACM-ICPC青岛现场赛 B题 Kawa Exam 题解 ZOJ 4059

    题意:BaoBao正在进行在线考试(都是选择题),每个题都有唯一的一个正确答案,但是考试系统有m个bug(就是有m个限制),每个bug表示为第u个问题和第v个问题你必须选择相同的选项,题目问你,如果你 ...

  2. zoj 3721 Final Exam Arrangement【贪心】

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3721 来源:http://acm.hust.edu.cn/vjudg ...

  3. [置顶] 2013_CSUST暑假训练总结

    2013-7-19 shu 新生训练赛:母函数[转换成了背包做的] shuacm 题目:http://acm.hdu.edu.cn/diy/contest_show.php?cid=20083总结:h ...

  4. Final Exam Arrangement(ZOJ)

    In Zhejiang University, there are N different courses labeled from 1 to N. Each course has its own t ...

  5. ZOJ 3795 Grouping(scc+最长路)

    Grouping Time Limit: 2 Seconds      Memory Limit: 65536 KB Suppose there are N people in ZJU, whose ...

  6. zoj 3795 Grouping tarjan缩点 + DGA上的最长路

    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practic ...

  7. ZOJ 3819 Average Score(平均分)

    Description 题目描述 Bob is a freshman in Marjar University. He is clever and diligent. However, he is n ...

  8. ZOJ 2819 Average Score 牡丹江现场赛A题 水题/签到题

    ZOJ 2819 Average Score Time Limit: 2 Sec  Memory Limit: 60 MB 题目连接 http://acm.zju.edu.cn/onlinejudge ...

  9. ZOJ 3687 The Review Plan I

    The Review Plan I Time Limit: 5000ms Memory Limit: 65536KB This problem will be judged on ZJU. Origi ...

随机推荐

  1. JAVA-Spring AOP五大通知类型

    一.前置通知 在目标方法执行之前执行的通知 在前置通知方法,可以没有参数,也可以额外接收一个JoinPoint,Spring会自动将该对象传入,代表当前的连接点,通过该对象可以获取目标对象和目标方法相 ...

  2. 从CNI到OVN

    kubernetes各版本离线安装包 诸如calico flannel等CNI实现,通过牺牲一些功能让网络复杂度得以大幅度降低是我极其推崇的,在云原生时代应用不再关心基础设施的场景下是一个明智之举,给 ...

  3. Python基础总结之异常、调试代码第十二天开始(新手可相互督促)

    年薪20万的梦想,加油! 我们在写代码的时候,控制台经常会报错,因为某种错误,导致我们的程序停止,且不再运行下面的代码. 我们看一个错误的代码示例: def add_1(): #没有参数 print( ...

  4. wamp不显示文件图标

    wamp不显示文件图标 效果如下图 右键图片"在新的标签页打开图片"后会跳转到404页面,并显示The requested URL /icons/unknown.gif was n ...

  5. Java性能权威指南读书笔记--之一

    JIT(即时编译) 解释型代码:程序可移植,相同的代码在任何有适当解释器的机器上,都能运行,但是速度慢. 编译型代码:速度快,电视不同CPU平台的代码无法兼容. java则是使用java的编译器先将其 ...

  6. 旁友数独会伐啦?python秒解数独了解下伐啦?

    前几天和隔壁邻居玩斗地主被发现了,牌被没收了,斗地主是斗不了了,但我还想和邻居玩耍.如果你还想斗斗地主,戳:趁老王不在,和隔壁邻居斗斗地主,比比大小 想破脑袋终于让我想到一个游戏,数独!什么叫数独?数 ...

  7. struts的上传下载

    文件上传 添加jar包 commons-io-1.3.2.jar commons-fileupload-1.2.1.jar 前台页面 form表单 method值为post 添加"encty ...

  8. springboot整合websocket原生版

    目录 HTTP缺点 HTTP websocket区别 websocket原理 使用场景 springboot整合websocket 环境准备 客户端连接 加入战队 微信公众号 主题 HTTP请求用于我 ...

  9. Java8 CompletableFuture 编程

    一.简介  所谓异步调用其实就是实现一个无需等待被调用函数的返回值而让操作继续运行的方法.在 Java 语言中,简单的讲就是另启一个线程来完成调用中的部分计算,使调用继续运行或返回,而不需要等待计算结 ...

  10. 既然synchronized是"万能"的,为什么还需要volatile呢?

    在我的博客和公众号中,发表过很多篇关于并发编程的文章,之前的文章中我们介绍过了两个在Java并发编程中比较重要的两个关键字:synchronized和volatile 我们简单回顾一下相关内容: 1. ...