题目大意:
  给你一个$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的更多相关文章

  1. 小程序-demo:天气预报

    ylbtech-小程序-demo:天气预报 1.返回顶部 1.app.js //app.js App({ //系统事件 onLaunch: function () {//小程序初始化事件 var th ...

  2. ACM ICPC China final G Pandaria

    目录 ACM ICPC China final G Pandaria ACM ICPC China final G Pandaria 题意:给一张\(n\)个点\(m\)条边的无向图,\(c[i]\) ...

  3. 【uwp】浅谈China Daily 中划词翻译的实现

    学习uwp开发也有一段时间了,最近上架了一个小应用(China Daily),现在准备将开发中所学到的一些东西拿出来跟大家分享交流一下. 先给出应用的下载链接:China Daily , 感兴趣的童鞋 ...

  4. 【英语学习】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 ...

  5. Ignite China 2015 之行

    微软首届Ignite China选择了金秋十月的北京,在顺义的九华山庄举办.这几天北京的空气特别好,再加上郊区高楼少,令人心胸开阔了不少.这次Ignite之行的任务有两个,其一是27号晚上与Windo ...

  6. hdu 5652 India and China Origins 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...

  7. 基于 React.js + Redux + Bootstrap 的 Ruby China 示例 (转)

    一直学 REACT + METEOR 但路由部分有点问题,参考一下:基于 React.js + Redux + Bootstrap 的 Ruby China 示例 http://react-china ...

  8. GTC China 2016观感

    上周二在北京参加了GTC China 2016,最大的感受就是一个字,“冷”!黄教主一如既往坚持机车皮夹克装,9月中旬的北京还没有那么的冷啊,感觉全场的空调简直是为他而开...好的,以上吐槽完毕,接着 ...

  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 ...

随机推荐

  1. io流中的装饰模式对理解io流的重要性

    为了说明 io流中的装饰者模式对理解io流的重要性,我想先简要介绍以下io的装饰模式. 装饰(decorator)你也可以翻译成修饰.比如:一个会精通化学数学的物理学家.在这个"物理学家&q ...

  2. js实现快速排序的方法

    因为面试面到了这个问题,所以写一下,加深印象,有两种方法 第一种是通过两个for循环,每一次对比相邻两个数据的大小,小的排在前面,如果前面的数据比后面的大就交换这两个数的位置,这个方法就是比较次数太多 ...

  3. Spring - IoC(2): 属性注入 & 构造注入

    依赖注入是指程序运行过程中,如果需要另外的对象协作(访问它的属性或调用它的方法)时,无须在代码中创建被调用者,而是依赖于外部容器的注入. 属性注入(Setter Injection) 属性注入是指 I ...

  4. 【hdu2222-Keywords Search】AC自动机基础裸题

    http://acm.hust.edu.cn/vjudge/problem/16403 题意:给定n个单词,一个字符串,问字符串中出现了多少个单词.(若单词her,he,字符串aher中出现了两个单词 ...

  5. 【Foreign】开锁 [概率DP]

    开锁 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input 4 5 1 2 5 4 3 1 5 ...

  6. javascript的阻塞机制

    javascript的阻塞机制 浏览器在执行javascript代码时,不能同时做其它事情,当遇到javascript时,浏览器会下载js文件,解析并执行该文件,而在这期间页面的渲染是完全被阻塞的,因 ...

  7. WIN8.1优化

    用WIN8.1至今,总结的所有优化的办法! 从Win7.Win8.1开始,微软为我们带来完善的系统服务.任务计划程序,这些都一定程度提升了日常使用,实现了“半自动化.半智能化”交互体验.但是对于高级用 ...

  8. [转]树莓派gpio口控制

    0.前言     树莓派现在越来越火,网上树莓派的资料也越来越多.树莓派源自英国,国外嵌入式开源领域具有良好的分享精神,树莓派各种集成库也层出不穷,下面推荐几个. [[开发语言]——python [[ ...

  9. Python阶段复习 - part 1 - Python基础练习题

    1.实现1-100的所有的和 # 方法1: sum = 0 for i in range(1,101): sum += i print(sum) # 方法2: num1 = int(input('请输 ...

  10. Swift中的类型属性(静态变量)

    http://blog.haohtml.com/archives/15098 Swift中的类型属性(静态变量) Posted on 2014/06/13 类型属性语法 在 C 或 Objective ...