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. http和WebSocket

    有关http和WebSocket 的区别网上有很多的质料. 个人在此仅仅是记录以下自己的学习心得,自己的理解. 1. http协议是用在应用层的协议,他是基于tcp协议的,http协议建立链接也必须要 ...

  2. Lua中数组的学习

    --数组的大小是不固定的 --一维数组的逻辑结构是线性表索引从1开始 array1 = {"Lua", "Tutorial"} , do print(array ...

  3. 关于网站的SYN_RECV(SYN_RECEIVED)***的防范措施

    关于网站的SYN_RECV(SYN_RECEIVED)***的防范措施 一.总结 一句话总结:SYN ***是最常见又最容易被利用的一种***手法.相信很多人还记得2000年YAHOO网站遭受的*** ...

  4. 雷林鹏分享:C# 正则表达式

    C# 正则表达式 正则表达式 是一种匹配输入文本的模式..Net 框架提供了允许这种匹配的正则表达式引擎.模式由一个或多个字符.运算符和结构组成. 定义正则表达式 下面列出了用于定义正则表达式的各种类 ...

  5. 3.4 复杂的x86指令举例

    计算机组成 3 指令系统体系结构 3.4 复杂的x86指令举例 x86作为复杂指令系统的代表,自然会有不少相当复杂的指令.在这一节我们将会看到其中有代表性的一些例子. 关于复杂的x86指令,我们这里举 ...

  6. Spring Cloud常用组件介绍

    一.Eureka (Netfix下) 云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移. 二.Spring Cloud Config (Spring下) 配置 ...

  7. Black Widow CodeForces - 704C (dp)

    大意: 给定一个m个bool变量的方程, 求方程解的个数 给定方程的形式类似于这样 每个括号是一个子式, 每个子式里变量数不超过2, 每个变量出现次数不超过2, 方程右侧一直是1 对每个变量出现的式子 ...

  8. 启动Eclipse时发生An internal error occurred during: "Initializing Java Tooling"错误

    详细提示如下: An internal error occurred during: "Initializing Java Tooling". Illegal exception ...

  9. 应用多种变形CSS3

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    < ...

  10. AIX的iostat命令解析(翻译红皮书)

    1.确定磁盘使用率 $ iostat -T 2 10System configuration: lcpu=8 drives=29 paths=52 vdisks=0tty:      tin      ...