Link
Title:
There is a picture with a path of different colors between each two points. A point can follow a color path to another point, indicating that there is a common color between the two points. There are
queries, ask
sum
There are several facial paths between the two points.
As shown in the figure below
, there are two between 1 and 2, one between 1 and 3, and one between 3 and 4.
Idea:
From the meaning of the question, we can know that only two paths with the same color between the two points can walk through. We can think about it first. If only one color can be used, it becomes the problem of whether it is reachable between two points. It is directly floyed to handle the transitive closure. It is now multiple colors. The problem is that between two points How many reachable paths are there, we compress a color path between two points to 1 bite, because the color may be 100, which is beyond the general integer, so use bitset to deal with,
in the first
-bit is 1 means there is a color between these two numbers
path, then we use floyed to handle transitive closure.
When the last query, just look at
There can be as many as 1 in .
bitset<102> w[101][101];
void floyed(int n){
for(int k = 1;k <=n;++k){
for(int i = 1;i <= n;++i){
for(int j = 1;j <= n;++j){
w[i][j] |= w[i][k]&w[k][j];
}
}
}
}
int mian(){
int n = read(),m = read();
rep(i,1,m){
int u = read() ,v = read(),k = read();
w[u][v].set(k,1);
w[v][u].set(k,1);
}
floyed(n);
int k = read();
while(k--){
int u = read(),v = read();
cout << w[u][v].count()<<endl;
}
}