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 ;
}