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.

Input

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

Input
4 5
1 2 1
1 2 2
2 3 1
2 3 3
2 4 3
3
1 2
3 4
1 4
Output
2
1
0
Input
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
Output
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(均摊复杂度)的更多相关文章

  1. Codeforces 506D Mr. Kitayuta's Colorful Graph(分块 + 并查集)

    题目链接  Mr. Kitayuta's Colorful Graph 把每种颜色分开来考虑. 所有的颜色分为两种:涉及的点的个数 $> \sqrt{n}$    涉及的点的个数 $<= ...

  2. CodeForces 505B Mr. Kitayuta's Colorful Graph

    Mr. Kitayuta's Colorful Graph Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

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

  4. DFS/并查集 Codeforces Round #286 (Div. 2) B - Mr. Kitayuta's Colorful Graph

    题目传送门 /* 题意:两点之间有不同颜色的线连通,问两点间单一颜色连通的路径有几条 DFS:暴力每个颜色,以u走到v为结束标志,累加条数 注意:无向图 */ #include <cstdio& ...

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

  6. codeforces 505B Mr. Kitayuta's Colorful Graph(水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Mr. Kitayuta's Colorful Graph Mr. Kitayut ...

  7. Codeforces Round #286 (Div. 1) D. Mr. Kitayuta's Colorful Graph

    D - Mr. Kitayuta's Colorful Graph 思路:我是暴力搞过去没有将答案离线,感觉将答案的离线的方法很巧妙.. 对于一个不大于sqrt(n) 的块,我们n^2暴力枚举, 对于 ...

  8. CodeForces - 505B Mr. Kitayuta's Colorful Graph 二维并查集

    Mr. Kitayuta's Colorful Graph Mr. Kitayuta has just bought an undirected graph consisting of n verti ...

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

随机推荐

  1. 时间常用api

    1.常用api 创建 Date 对象  -  年  -  月  -  日   -  小时  -  分  -  秒 -  星期 var now=new Date() var year = now.get ...

  2. logback的使用和logback.xml详解[转]

    一.logback的介绍 Logback是由log4j创始人设计的另一个开源日志组件,官方网站: http://logback.qos.ch.它当前分为下面下个模块: logback-core:其它两 ...

  3. 牛客练习赛7 E 珂朵莉的数列

    珂朵莉的数列 思路: 树状数组+高精度 离散化不知道哪里写错了,一直wa,最后用二分写的离散化 哪位路过大神可以帮我看看原来的那个离散化错在哪里啊 通过代码: import java.math.Big ...

  4. JSON自定义排序

    var json=[{ Name:'张三', Addr:'重庆', Age:'20' },{ Name:'张三3', Addr:'重庆2', Age:'25' },{ Name:'张三2', Addr ...

  5. 百度echart如何动态生成图表

    百度echart如何动态生成图表 一.总结 一句话总结: clear hideloading setOption 主要是下面三行代码: myChart.clear(); //清空画布myChart.h ...

  6. 雷林鹏分享:C# 命名空间(Namespace)

    C# 命名空间(Namespace) 命名空间的设计目的是为了提供一种让一组名称与其他名称分隔开的方式.在一个命名空间中声明的类的名称与另一个命名空间中声明的相同的类的名称不冲突. 定义命名空间 命名 ...

  7. JELLYFISH - Fast, Parallel k-mer Counting for DNA

    kmer分析其实是非常耗费计算资源的,如果我们自己写脚本来分析kmer的话,首先要将所有的序列打断成一定长度的kmer,然后将所有的kmer存储起来,最后统计每个kmer出现的频率,或者统计出现指定次 ...

  8. php-------代码加密的几种方法

    代码加密,也是保护网站安全的一种方法,以下我们来介绍一下如何通过PHP的自定义函数来加密我们的PHP代码. 方法一: <?php function encode_file_contents($f ...

  9. SPFA 最短路

    求单源最短路的SPFA算法的全称是:Shortest Path Faster Algorithm.     SPFA算法是西南交通大学段凡丁于1994年发表的.    从名字我们就可以看出,这种算法在 ...

  10. 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...