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. XML_CPP_资料_libxml2库函数详解

    http://blog.csdn.net/hanchaoman/article/details/42557195 许多事物符合80/20法则,libxml中也是20%的函数提供了80%的功能.下面的列 ...

  2. Qt5.3.2_CentOS6.4_x86_调试源码关联【勿删,简洁】

    1. Qt5.3.2 --> Tools --> Options... --> 左侧选择"Debugger" --> 然后选择"General&q ...

  3. SSD论文理解

    SSD论文贡献: 1. 引入了一种单阶段的检测器,比以前的算法YOLO更准更快,并没有使用RPN和Pooling操作: 2. 使用一个小的卷积滤波器应用在不同的feature map层从而预测BB的类 ...

  4. C++指针详解(转)

    指针的概念 指针是一个特殊的变量,它里面存储的数值被解释成为内存里的一个地址.要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的类型,指针的值或者叫指针所指向的内存区,还有指针本身所占 ...

  5. Spring Boot中注解事务

  6. union和union all比较说明

    执行sql语句:select '1' union select '3' union select '2'  union select '1' 得到的结果集如下: 执行sql语句如下: select ' ...

  7. ccf消除类游戏

    问题描述 消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行m列的游戏棋盘上进行,棋盘的每一行每一列的方格上放着一个有颜色的棋子,当一行或一列上有连续三个或更多的相同颜色的棋子时,这些棋子都被消 ...

  8. SQL Server数据库入门学习总结

    数据库基本是由表,关系,操作组成:对于初学者,首先要学的是: 1.数据库是如何存储数据的 —— 表.约束.触发器 2.数据库是如何操作数据的 —— insert,update,delete.T-sql ...

  9. hibernate中删除表遇到主键被外键引用违反完整约束条件不能删除的问题

    MySQL在InnoDB中设置了foreign key关联,造成无法更新或删除数据.可以通过设置FOREIGN_KEY_CHECKS变量来避免这种情况.  SET FOREIGN_KEY_CHECKS ...

  10. 如何获取显示器的EDID信息

    Q1: 为什么要写这篇文章? A1:在最近的工作中遇到了不少问题,其中很多都是和EDID相关的.可以说,作为一家以“显示”为生的企业,我们时时刻刻在与EDID打交道.EDID这东西很简单,但是如果不了 ...