[Gym101194G][CHINA-Final2016]Pandaria
题目大意:
给你一个$n(n\le10^5)$个点,$m(m\le2\times10^5)$条边的无向图,每个点有一个颜色$c_i$,每条边有一个边权$w_i$。$q(q\le2\times10^5)$组询问$(x,w)$,每次询问从点$x$出发,只经过边权不超过$w$的边所能到达的连通块中,出现次数最多的颜色中,编号最小的颜色是多少?
思路:
Kruskal重构树+线段树合并。时间复杂度$O(m\log m+q\log n)$。
#include<cstdio>
#include<cctype>
#include<climits>
#include<cstring>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=2e5,logN=,M=2e5;
int n,m,tot,ch[N][],val[N]={INT_MAX},ans[N];
struct Edge {
int u,v,w;
bool operator < (const Edge &another) const {
return w<another.w;
}
};
Edge e[M];
struct DisjointSet {
int anc[N];
void reset() {
for(register int i=;i<N;i++) anc[i]=i;
}
int find(const int &x) {
return x==anc[x]?x:anc[x]=find(anc[x]);
}
void merge(const int &x,const int &y) {
anc[find(x)]=find(y);
}
bool same(const int &x,const int &y) {
return find(x)==find(y);
}
};
DisjointSet s;
inline void kruskal() {
tot=n;
s.reset();
std::sort(&e[],&e[m]);
for(register int i=;i<m;i++) {
const int &u=e[i].u,&v=e[i].v,&w=e[i].w;
if(!s.same(u,v)) {
val[++tot]=w;
ch[tot][]=s.find(u);
ch[tot][]=s.find(v);
s.merge(u,tot);
s.merge(v,tot);
}
}
}
class SegmentTree {
private:
struct Node {
int max,val,left,right;
};
Node node[N/*logN];
int new_node(const int &p) {
node[++root[]]=node[p];
return root[];
}
void push_up(const int &p) {
if(node[node[p].left].max>=node[node[p].right].max) {
node[p].max=node[node[p].left].max;
node[p].val=node[node[p].left].val;
} else {
node[p].max=node[node[p].right].max;
node[p].val=node[node[p].right].val;
}
}
public:
int root[N];
void reset() {
memset(root,,sizeof root);
}
void modify(int &p,const int &b,const int &e,const int &x) {
p=new_node(p);
if(b==e) {
node[p]=(Node){,x,,};
return;
}
const int mid=(b+e)>>;
if(x<=mid) modify(node[p].left,b,mid,x);
if(x>mid) modify(node[p].right,mid+,e,x);
push_up(p);
}
int merge(const int &p,const int &q,const int &b,const int &e) {
if(!p||!q) return p|q;
if(b==e) {
node[p].max+=node[q].max;
return p;
}
const int mid=(b+e)>>;
node[p].left=merge(node[p].left,node[q].left,b,mid);
node[p].right=merge(node[p].right,node[q].right,mid+,e);
push_up(p);
return p;
}
int query(const int &p) const {
return node[p].val;
}
};
SegmentTree t;
int dep[N],anc[N][logN];
inline int log2(const float &x) {
return ((unsigned&)x>>&)-;
}
void dfs(const int &x,const int &par) {
dep[x]=dep[anc[x][]=par]+;
for(register int i=;i<=log2(dep[x]);i++) {
anc[x][i]=anc[anc[x][i-]][i-];
}
if(x<=n) return;
dfs(ch[x][],x);
dfs(ch[x][],x);
t.root[x]=t.merge(t.root[ch[x][]],t.root[ch[x][]],,n);
ans[x]=t.query(t.root[x]);
}
inline int find(int x,const int &w) {
for(register int i=log2(dep[x]);~i;i--) {
if(val[anc[x][i]]<=w) x=anc[x][i];
}
return x;
}
int main() {
const int T=getint();
for(register int i=;i<=T;i++) {
t.reset();
n=getint(),m=getint();
for(register int i=;i<=n;i++) {
t.modify(t.root[i],,n,ans[i]=val[i]=getint());
}
for(register int i=;i<m;i++) {
const int u=getint(),v=getint(),w=getint();
e[i]=(Edge){u,v,w};
}
kruskal();
dfs(tot,);
printf("Case #%d:\n",i);
for(register int q=getint(),last=;q;q--) {
const int x=getint()^last,w=getint()^last;
printf("%d\n",last=ans[find(x,w)]);
}
}
return ;
}
[Gym101194G][CHINA-Final2016]Pandaria的更多相关文章
- 小程序-demo:天气预报
ylbtech-小程序-demo:天气预报 1.返回顶部 1.app.js //app.js App({ //系统事件 onLaunch: function () {//小程序初始化事件 var th ...
- ACM ICPC China final G Pandaria
目录 ACM ICPC China final G Pandaria ACM ICPC China final G Pandaria 题意:给一张\(n\)个点\(m\)条边的无向图,\(c[i]\) ...
- 【uwp】浅谈China Daily 中划词翻译的实现
学习uwp开发也有一段时间了,最近上架了一个小应用(China Daily),现在准备将开发中所学到的一些东西拿出来跟大家分享交流一下. 先给出应用的下载链接:China Daily , 感兴趣的童鞋 ...
- 【英语学习】2016.09.11 Culture Insider: Teacher's Day in ancient China
Culture Insider: Teacher's Day in ancient China 2016-09-10 CHINADAILY Today is the 32nd Chinese Te ...
- Ignite China 2015 之行
微软首届Ignite China选择了金秋十月的北京,在顺义的九华山庄举办.这几天北京的空气特别好,再加上郊区高楼少,令人心胸开阔了不少.这次Ignite之行的任务有两个,其一是27号晚上与Windo ...
- hdu 5652 India and China Origins 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...
- 基于 React.js + Redux + Bootstrap 的 Ruby China 示例 (转)
一直学 REACT + METEOR 但路由部分有点问题,参考一下:基于 React.js + Redux + Bootstrap 的 Ruby China 示例 http://react-china ...
- GTC China 2016观感
上周二在北京参加了GTC China 2016,最大的感受就是一个字,“冷”!黄教主一如既往坚持机车皮夹克装,9月中旬的北京还没有那么的冷啊,感觉全场的空调简直是为他而开...好的,以上吐槽完毕,接着 ...
- [Fraud] China UnionPay defrauded in Macau money laundering scandal
Source: http://www.wantchinatimes.com/news-subclass-cnt.aspx?id=20140510000005&cid=1103 China Un ...
随机推荐
- CRM系统主要业务流程思维导图
[CRM五策略] ❶对客户进行分类,不是根据规模,而是根据和你的关系,越细腻越好: ❷不定期更新客户资料,信息越全面越好: ❸主动对客户进行 ...
- Linux下rsync 安装与配置
1.什么是rsync Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“Rsync算法”来使本地和远 程两个 ...
- php多虚拟主机配置
一.配置httpd.conf# Virtual hosts#Include conf/extra/httpd-vhosts.conf //取消这一行的# 二.配置httpd-vhosts. ...
- 解决mysql报错:- Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ'
mysql执行报错: - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated c ...
- 我喜欢的4个VS扩展吧
原文发布时间为:2011-06-09 -- 来源于本人的百度文章 [由搬家工具导入]
- 【洛谷 P3304】[SDOI2013]直径(树的直径)
题目链接 题意,求一棵树被所有直径经过的边的条数. 这题是我们8.25KS图论的最后一题,当时我果断打了暴力求所有直径然后树上差分统计的方法,好像有点小问题,boom0了. 考完改这题,改了好久,各种 ...
- Linux 添加PPA源
我们在使用Ubuntu安装程序时经常会遇到添加软件源的操作,最常见的是ppa软件源. 例如:sudo add-apt-repository ppa:rvm/smplayer 这就是添加smplayer ...
- CentOS 7 安装python3.6.1
说明:CentOS 7最小化安装,默认Python 2.7 ,开发需要安装python3.6.1 . 1.检查python版本 [root@bogon ~]# python -VPython 2.7. ...
- wxpython学习资源
http://www.cnblogs.com/dyx1024/archive/2012/07/15/2592202.html wxPython:布局管理器sizer介绍 ogs.com/dyx1024 ...
- HDU1024(最大M子段和)
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...