题目大意:
  给你一个$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. Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph

    D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...

  2. grub ubuntu启动

    set root=(hd0,gpt10) 现在变为 gpt9 了 安装固态后.变成了 (hd1,gpt11) set prefix=(hd0,gpt10)/boot/grub insmod norma ...

  3. Awk basic and practice

    定义:Awk是一种程序语言,用来处理数据和产生报告.数据可来自标准输入,文件,管道输出. 格式:#awk '/pattern/ {action}' filename 术语:pattern, 样式是由正 ...

  4. 转:LVS负载均衡

    1.什么是LVS? 首 先简单介绍一下LVS (Linux Virtual Server)到底是什么东西,其实它是一种集群(Cluster)技术,采用IP负载均衡技术和 基于内容请求分发技术.调度器具 ...

  5. [Codevs1519]过路费解题报告|最小生成树|LCA

    在某个遥远的国家里,有 n个城市.编号为 1,2,3,…,n.这个国家的政府修建了m 条双向道路,每条道路连接着两个城市.政府规定从城市 S 到城市T需要收取的过路费为所经过城市之间道路长度的最大值. ...

  6. 【Luogu P3834】可持久化数组(可持久化线段树)

    题目链接 可持久化线段树模板题. 这里总结一下可持久化线段树. 可持久化数据结构就是能恢复历史状态的数据结构,比如可持久化\(Trie\),并查集,平衡树. 可持久化数组是最基础的,这里通过可持久化线 ...

  7. TensorFlow_曲线拟合

    # coding:utf-8 import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import os ...

  8. bzoj 1067 特判

    这道题的大题思路就是模拟 假设给定的年份是x,y,首先分为4个大的情况,分别是 x的信息已知,y的信息已知 x的信息已知,y的信息未知 x的信息未知,y的情况已知 x的信息未知,y的情况未知 然后对于 ...

  9. [Leetcode Week9]Word Break

    Word Break 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/word-break/description/ Description Given ...

  10. 一次向svn中增加所有新增文件 svn add all new files【转】

    以下摘自:<卓有成效的程序员>之自动化 转自:http://blog.csdn.net/spare_h/article/details/6677435 我经常会一次往Subversion里 ...