Mr. Kitayuta's Colorful Graph CodeForces - 506D(均摊复杂度)
Mr. Kitayuta has just bought an undirected graph with n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi.
Mr. Kitayuta wants you to process the following q queries.
In the i-th query, he gives you two integers - ui and vi.
Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly.
The first line of the input contains space-separated two integers - n and m(2 ≤ n ≤ 105, 1 ≤ m ≤ 105), denoting the number of the vertices and the number of the edges, respectively.
The next m lines contain space-separated three integers - ai, bi(1 ≤ ai < bi ≤ n) and ci(1 ≤ ci ≤ m).
Note that there can be multiple edges between two vertices. However,
there are no multiple edges of the same color between two vertices, that
is, if i ≠ j, (ai, bi, ci) ≠ (aj, bj, cj).
The next line contains a integer- q(1 ≤ q ≤ 105), denoting the number of the queries.
Then follows q lines, containing space-separated two integers - ui and vi(1 ≤ ui, vi ≤ n). It is guaranteed that ui ≠ vi.
Output
For each query, print the answer in a separate line.
Examples
4 5
1 2 1
1 2 2
2 3 1
2 3 3
2 4 3
3
1 2
3 4
1 4
2
1
0
5 7
1 5 1
2 5 1
3 5 1
4 5 1
1 2 2
2 3 2
3 4 2
5
1 5
5 1
2 5
1 5
1 4
1
1
1
1
2
Note
Let's consider the first sample.
The figure above shows the first sample.
- Vertex 1 and vertex 2 are connected by color 1 and 2.
- Vertex 3 and vertex 4 are connected by color 3.
- Vertex 1 and vertex 4 are not connected by any single color.
题意:给定N点M边无向图,每边有自己的颜色,Q次询问,每次给出(u,v),询问多少种颜色,使得u和v连通。
思路:排序,同一种颜色同时处理,然后离线回答每个询问,但是有可以一个点存在M种颜色里,而且存在Q次询问里,所以最坏情况是M*Q*log。log是并查集的复杂度。所以要加均摊。 如果一种颜色的点比较多,就上面那么回答; 否则,我们就暴力记录点对。
总的复杂度是Q*sqrt(N)*log(N); 4s可以过了; 实际上只跑了500ms,还可以。
#include<bits/stdc++.h>
#define pii pair<int,int>
#define mp make_pair
#define F first
#define S second
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn],To[maxn],id[maxn],ans[maxn],cnt;
int a[maxn],b[maxn],fa[maxn],times[maxn],T,q[maxn],tot,N;
map<pii,int>Mp;
map<pii,int>fcy;
struct in{
int u,v,col;
bool friend operator <(in w,in v){ return w.col<v.col; }
}s[maxn];
void add(int u,int v,int o)
{
Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v; id[cnt]=o;
}
int find(int x){
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
}
void merge(int x,int y)
{
int fx=find(x),fy=find(y);
fa[fx]=fy;
}
int main()
{
int M,Q,u,v;
scanf("%d%d",&N,&M);
rep(i,,M) scanf("%d%d%d",&s[i].u,&s[i].v,&s[i].col);
sort(s+,s+M+);
scanf("%d",&Q);
rep(i,,Q){
scanf("%d%d",&a[i],&b[i]); if(a[i]>b[i]) swap(a[i],b[i]);
add(a[i],b[i],i);
fcy[mp(a[i],b[i])]=;
}
rep(i,,M){
int j=i; T++; merge(s[i].u,s[i].v);
while(j+<=M&&s[j+].col==s[i].col) j++;
tot=;
rep(k,i,j) q[++tot]=s[k].u,q[++tot]=s[k].v;
sort(q+,q+tot+); tot=unique(q+,q+tot+)-(q+);
rep(k,,tot) fa[q[k]]=q[k],times[q[k]]=T;
rep(k,i,j) merge(s[k].u,s[k].v);
if(tot>sqrt(N)) rep(k,,tot) {
for(int w=Laxt[q[k]];w;w=Next[w]){
if(times[To[w]]==T&&find(q[k])==find(To[w])) ans[id[w]]++;
}
}
else {
rep(k,,tot)
rep(p,k+,tot){
if(find(q[k])==find(q[p])&&fcy.find(mp(q[k],q[p]))!=fcy.end()) Mp[mp(q[k],q[p])]++;
}
}
i=j;
}
rep(i,,Q) printf("%d\n",ans[i]+Mp[mp(a[i],b[i])]);
return ;
}
可撤销并查集版本,530ms
#include<bits/stdc++.h>
#define pii pair<int,int>
#define mp make_pair
#define F first
#define S second
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn],To[maxn],id[maxn],ans[maxn],cnt;
int a[maxn],b[maxn],fa[maxn],times[maxn],T,q[maxn],tot,N;
map<pii,int>Mp,fcy;
struct in{
int u,v,col;
bool friend operator <(in w,in v){ return w.col<v.col; }
}s[maxn];
void add(int u,int v,int o)
{
Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v; id[cnt]=o;
}
int find(int x){
if(times[x]!=T) times[x]=T, fa[x]=x;
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
}
void merge(int x,int y)
{
int fx=find(x),fy=find(y);
fa[fx]=fy;
}
int main()
{
int M,Q,u,v;
scanf("%d%d",&N,&M);
rep(i,,M) scanf("%d%d%d",&s[i].u,&s[i].v,&s[i].col);
sort(s+,s+M+);
scanf("%d",&Q);
rep(i,,Q){
scanf("%d%d",&a[i],&b[i]); if(a[i]>b[i]) swap(a[i],b[i]);
add(a[i],b[i],i);
fcy[mp(a[i],b[i])]=;
}
rep(i,,M){
int j=i; T++; merge(s[i].u,s[i].v);
while(j+<=M&&s[j+].col==s[i].col) j++;
tot=;
rep(k,i,j) q[++tot]=s[k].u,q[++tot]=s[k].v,merge(s[k].u,s[k].v);;
sort(q+,q+tot+); tot=unique(q+,q+tot+)-(q+);
if(tot>sqrt(N)) rep(k,,tot) {
for(int w=Laxt[q[k]];w;w=Next[w]){
if(times[To[w]]==T&&find(q[k])==find(To[w])) ans[id[w]]++;
}
}
else {
rep(k,,tot)
rep(p,k+,tot){
if(find(q[k])==find(q[p])&&fcy.find(mp(q[k],q[p]))!=fcy.end()) Mp[mp(q[k],q[p])]++;
}
}
i=j;
}
rep(i,,Q) printf("%d\n",ans[i]+Mp[mp(a[i],b[i])]);
return ;
}
Mr. Kitayuta's Colorful Graph CodeForces - 506D(均摊复杂度)的更多相关文章
- Codeforces 506D Mr. Kitayuta's Colorful Graph(分块 + 并查集)
题目链接 Mr. Kitayuta's Colorful Graph 把每种颜色分开来考虑. 所有的颜色分为两种:涉及的点的个数 $> \sqrt{n}$ 涉及的点的个数 $<= ...
- CodeForces 505B Mr. Kitayuta's Colorful Graph
Mr. Kitayuta's Colorful Graph Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph 并查集
D. Mr. Kitayuta's Colorful Graph Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/ ...
- DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph
题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...
- Codeforces Round #286 (Div. 2) B. Mr. Kitayuta's Colorful Graph dfs
B. Mr. Kitayuta's Colorful Graph time limit per test 1 second memory limit per test 256 megabytes in ...
- codeforces 505B Mr. Kitayuta's Colorful Graph(水题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...
- Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph
D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...
- CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集
Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n verti ...
- B. Mr. Kitayuta's Colorful Graph
B. Mr. Kitayuta's Colorful Graph time limit per test 1 second Mr. Kitayuta has just bought an undi ...
随机推荐
- 51nod 1682 中位数计数(前缀和)
51nod 1682 中位数计数 思路: sum[i]表示到i为止的前缀和(比a[i]小的记为-1,相等的记为0,比a[i]大的记为1,然后求这些-1,0,1的前缀和): hash[sum[i]+N] ...
- C++编程模板2
C++编程模板2 #include <iostream> using namespace std; /* */ int main(){ int ans; printf("%d\n ...
- Dalvik VM (DVM) 与Java VM (JVM) 的区别?
Dalvik虚拟机存在于Android系统,JVM是java虚拟机,两者都是虚拟机,本文就对两者进行比较,讲述它们的不同. Dalvik虚拟机是Google等厂商合作开发的Android移动设备平台的 ...
- English trip V1 - 2.Don't Do That Teacher:Patrick Key: 祈使句(imperatives)
什么是祈使句? What's imperatives? 求或者希望别人做什么事或者不做什么事时用的句子:带有命令的语气 In this lesson you will learn how to ...
- fzu1901 kmp
For each prefix with length P of a given string S,if S[i]=S[i+P] for i in [0..SIZE(S)-p-1], then the ...
- thinkphp导出报表
这是我写的一个方法,这个方法可以直接使用在你的代码上.下面我画红色的就是要修改或者删除的.public function import(){ /*创建PHPEXCLE读取,默认excel2007,最好 ...
- yarn的淘宝镜像
现在有很多人使用npm但是很多人也开始使用了更快的更方便额的yarn,可是yarn也有下载速度慢,容易被墙的担忧~又不能像npm那样直接安装淘宝镜像,所以就有了更改yarn的yarn源~ yarn c ...
- 双机热备(准)-->RAC(夭折)-->DG(异地容灾)
以下有的地方为oracle专业术语,非懂勿喷.前段时间某项目负责人告知,他们应用需要一套oracle数据库环境运行模式为双机热备.简单了解下对于现在已经非常成熟的RAC再合适不过了.详细问了问当前服务 ...
- 46. 47. Permutations
求全排列. 1. 无重复元素 Given a collection of distinct numbers, return all possible permutations. For example ...
- Sublime text3配置xdebug调试记录
第一次配置遇到的问题记录: 问题:配置php.ini的时候xdebug.remote_port = 9001刚开始我一直配置9000端口冲突,然后一切弄好了访问浏览器就一直在转圈无法访问: 现在开始配 ...