题目大意:
  给你一个$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. POJ2516:Minimum Cost(最小费用最大流)

    Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19088   Accepted: 6740 题目链 ...

  2. POJ 1050 To the Max 二维最大子段和

    To the MaxTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 52281 Accepted: 27633Description ...

  3. Eclipse中的Web项目自动部署到Tomcat的webapp目录下

    Eclipse中的Web项目自动部署到Tomcat   原因 很长时间没用Eclipse了,近期由于又要用它做个简单的JSP项目,又要重新学习了,虽然熟悉的很快,但记忆总是很模糊,偶尔犯错,以前很少写 ...

  4. 深入探索 高效的Java异常处理框架

    转载自:http://www.sunwei.org/archives/196 摘要:本文从Java异常最基本的概念.语法开始讲述了Java异常处理的基本知识,分析了Java异常体系结构,对比Sprin ...

  5. eclipse+jetty+web项目调试---不显示源码

    本人eclipse版本:JUNO 1.问题现象:显示源码时,不显示箭头(指示到哪行) 解决办法: debug configurations  --->Goals设置参数  clean -X je ...

  6. session超时设置+超时页面跳转

    session超时设置,方法有三种: (1)在主页面或者公共页面中加入:session.setMaxInactiveInterval(600);参数600单位是秒,即在没有10分钟活动后,sessio ...

  7. [POJ1286&POJ2154&POJ2409]Polya定理

    Polya定理 L=1/|G|*(m^c(p1)+m^c(p2)+...+m^c(pk)) G为置换群大小 m为颜色数量 c(pi)表示第i个置换的循环节数 如置换(123)(45)(6)其循环节数为 ...

  8. python面向对象进阶(上)

    一 .isinstance(obj,cls)和issubclass(sub,super) (1)isinstance(obj,cls)检查对象obj是否是类 cls 的对象,返回True和Flase ...

  9. Hadoop简单源码样例

    1.WordCount策略比较简单 import java.io.IOException; import java.util.StringTokenizer; import org.apache.ha ...

  10. C#区分大小写

    连属性也是要区分大小写的,如 获取数据长度 错误:strs.length 这样是报错的 正确:strs.Length