给你一张图,求顶点u到v有多少种颜色的路径
判断连通性:floyd,并查集,dfs,bfs
先枚举颜色,然后判断这种颜色的路径是否存在
#include<iostream> #include<cstring> #include<algorithm> #define rep(i,a,b) for(int i=a;i<b;++i) #define repd(i,a,b) for(int i=a;i<=b;++i) #define mes(s,c) memset(s,c,sizeof(s)) const int maxn=110; using namespace std; int mp[maxn][maxn][maxn]; int vis[maxn]; int n,m; bool dfs(int clr,int x,int y) { vis[x]=1; if(x==y) return true; repd(i,1,n){ if(!vis[i]&&mp[clr][x][i]){ if(dfs(clr,i,y)) return true; } } return false; } int main() { int q,x,y,c; cin>>n>>m; rep(i,0,m){ cin>>x>>y>>c; mp[c][x][y]=mp[c][y][x]=1; } cin>>q; while(q--){ int sum=0; cin>>x>>y; rep(i,1,maxn){ mes(vis,0); sum+=dfs(i,x,y); } cout<<sum<<endl; } return 0; }
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #define rep(i,a,b) for(int i=a;i<b;++i) #define rep_d(i,a,b) for(int i=a;i<=b;++i) #define mes(s,c) memset(s,c,sizeof(s)) const int maxn=101; using namespace std; int n,m; int fa[maxn][maxn]; int Find(int c,int x) { return fa[c][x]==x?x:fa[c][x]=Find(c,fa[c][x]); } int main() { int x,y,c,q; cin>>n>>m; rep_d(i,1,m)for(int j=1;j<=n;++j){ fa[i][j]=j; } rep_d(i,1,m){ cin>>x>>y>>c; int f_x=Find(c,fa[c][x]); int f_y=Find(c,fa[c][y]); fa[c][f_x]=f_y; }cin>>q; while(q--){ int ans=0; cin>>x>>y; rep(i,1,m){ if(Find(i,fa[i][x])==Find(i,fa[i][y])) ans++; }cout<<ans<<endl; } return 0; }
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> #define rep(i,a,b) for(int i=a;i<b;++i) #define rep_d(i,a,b) for(int i=a;i<=b;++i) #define mes(s,c) memset(s,c,sizeof(s)) const int maxn=101; using namespace std; int n,m; int vis[maxn]; struct node { int v,w,next; }edge[maxn<<1];int head[maxn],tot; inline void addedge(int u,int v,int w){edge[tot].v=v,edge[tot].w=w,edge[tot].next=head[u],head[u]=tot++;} bool bfs(int st,int ed,int c) { queue<int> q; q.push(st); bool vis[maxn]; mes(vis,0); vis[st]=1; while(!q.empty()){ int u=q.front();q.pop(); if(u==ed) return true; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].v; if(edge[i].w==c&&!vis[v]){ vis[v]=1; q.push(v); } } } return false; } int main() { memset(head,-1,sizeof(head)); cin>>n>>m; int x,y,c,q; rep(i,0,m){ cin>>x>>y>>c; addedge(x,y,c); addedge(y,x,c); }cin>>q; while(q--){ cin>>x>>y;mes(vis,0); int ans=0; for(int i=head[x];i!=-1;i=edge[i].next){ if(!vis[c=edge[i].w]){ ans+=bfs(x,y,c); vis[c]=1; } } cout<<ans<<endl; } return 0; }
#include<iostream> #include<cstring> #include<cstdio> #define rep(i,n) for(int i=0;i<n;++i) #define rep1(i,a,b) for(int i=a;i<b;++i) #define mes(s,c) memset(s,c,sizeof(s)) const int maxn=101; const int INF=1<<29; using namespace std; int n,m; int g[maxn][maxn][maxn]; void Init() { for(int x=1; x<=m; ++x) { for(int i=1; i<=n; ++i) { for(int j=1; j<=n; ++j) g[x][i][j]=INF; } } } void change() { for(int x=1; x<=m; ++x) { for(int k=1; k<=n; ++k) { for(int i=1; i<=n; ++i) { for(int j=1; j<=n; ++j) if(g[x][i][j]>g[x][i][k]+g[x][k][j]) { g[x][i][j]=g[x][i][k]+g[x][k][j]; } } } } } int main() { cin>>n>>m; Init(); int x,y,c; rep1(i,0,m) { cin>>x>>y>>c; g[c][x][y]=g[c][y][x]=1; } change(); int q; cin>>q; while(q--) { cin>>x>>y; int ans=0; for(int i=1; i<=m; ++i) { if(g[i][x][y]!=INF) { // cout<<"11111111=="<<g[i][x][y]<<endl; ans++; } } cout<<ans<<endl; } return 0; }