https://codeforces.com/gym/101808/problem/K
题意:给出n个点n条边的无向连通图,m次询问u、v间的最短路径。
解法:n条边减去一条即为树。所以找出成环的边为a , b , l ;
u 、 v两点间有三种路径取最短即可:
1、dis(u , v)
2、dis(u , a)+l+dis(b , v)
3、dis(u , b)+l+dis(a , v);
倍增求最短路。
//#include<bits/stdc++.h> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <string> #include <stdio.h> #include <queue> #include <stack> #include <map> #include <set> #include <string.h> #include <vector> typedef long long ll ; #define mod 1000000007 #define gcd __gcd #define rep(i , j , n) for(int i = j ; i <= n ; i++) #define red(i , n , j) for(int i = n ; i >= j ; i--) #define ME(x , y) memset(x , y , sizeof(x)) //ll lcm(ll a , ll b){return a*b/gcd(a,b);} //ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;} //int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;} //const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len} #define SC scanf #define INF 0x3f3f3f3f #define PI acos(-1) #define pii pair<int,int> #define fi first #define se second #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define int ll using namespace std; const int maxn = 1e5+9; int head[maxn] , tol , fa[maxn][22] , de[maxn] , dis[maxn] , f[maxn]; int a , b , l; int n , m; struct node{ int to , next , w; }g[maxn<<1]; void add(int u , int v , int w){ g[++tol] = {v , head[u] , w}; head[u] = tol; } int find(int x){ return x == f[x] ? x : find(f[x]); } void unite(int x , int y){ x = find(x) , y = find(y); f[x] = y; } void init(){ ME(head , 0); ME(fa , 0); ME(de , 0); tol = 0; rep(i , 1 , n){ f[i] = i ; } } void dfs(int u , int pre , int d){ de[u] = de[pre]+1; dis[u] = d ; fa[u][0] = pre; for(int i = 1 ; (1<<i) <= de[u] ; i++){ fa[u][i] = fa[fa[u][i-1]][i-1]; } for(int i = head[u] ; i ; i = g[i].next){ int v = g[i].to; if(v == pre) continue; dfs(v , u , d+g[i].w); } } int lca(int u , int v){ if(de[u] < de[v]) swap(u , v); red(i , 20 , 0){ if(de[u] - (1<<i) >= de[v]){ u = fa[u][i]; } } if(u == v) return v ; red(i , 20 , 0){ if(fa[u][i] != fa[v][i]){ u = fa[u][i]; v = fa[v][i]; } } return fa[u][0]; } int get_dis(int u , int v){ return dis[u] + dis[v] - 2 * dis[lca(u , v)]; } void solve(){ cin >> n >> m ; init(); rep(i , 1 , n){ int u , v , w; cin >> u >> v >> w ; if(find(u) != find(v)){ add(u , v , w); add(v , u , w); unite(u , v); }else{ a = u , b = v ; l = w ; } } dfs(1 , 0 , 0); rep(i , 1 , m){ int u , v ; cin >> u >> v ; int ans = get_dis(u , v); ans = min(ans , get_dis(u , a)+l+get_dis(b , v)); ans = min(ans , get_dis(u , b)+l+get_dis(a , v)); cout << ans << endl; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t ; cin >> t ; while(t--){ solve(); } }
https://codeforces.com/contest/1304/problem/E
题意:给出一n节点的颗树,m此询问:增加一条边a到b,能否满足u经过w条边(每条边可以走任意次)最终到达v。
解法:与上一题一样,只是稍稍该变问的方式,只要三条最短路径的一条dis<=w&&dis%2==w%2,则满足条件。
//#include<bits/stdc++.h> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> #include <string> #include <stdio.h> #include <queue> #include <stack> #include <map> #include <set> #include <string.h> #include <vector> typedef long long ll ; #define mod 1000000007 #define gcd __gcd #define rep(i , j , n) for(int i = j ; i <= n ; i++) #define red(i , n , j) for(int i = n ; i >= j ; i--) #define ME(x , y) memset(x , y , sizeof(x)) //ll lcm(ll a , ll b){return a*b/gcd(a,b);} //ll quickpow(ll a , ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;b>>=1,a=a*a%mod;}return ans;} //int euler1(int x){int ans=x;for(int i=2;i*i<=x;i++)if(x%i==0){ans-=ans/i;while(x%i==0)x/=i;}if(x>1)ans-=ans/x;return ans;} //const int N = 1e7+9; int vis[n],prime[n],phi[N];int euler2(int n){ME(vis,true);int len=1;rep(i,2,n){if(vis[i]){prime[len++]=i,phi[i]=i-1;}for(int j=1;j<len&&prime[j]*i<=n;j++){vis[i*prime[j]]=0;if(i%prime[j]==0){phi[i*prime[j]]=phi[i]*prime[j];break;}else{phi[i*prime[j]]=phi[i]*phi[prime[j]];}}}return len} #define SC scanf #define INF 0x3f3f3f3f #define PI acos(-1) #define pii pair<int,int> #define fi first #define se second #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define int ll using namespace std; const int maxn = 1e5+9; int head[maxn] , tol , fa[maxn][22] , de[maxn] , f[maxn]; int n , m; struct node{ int to , next; }g[maxn<<1]; void add(int u , int v){ g[++tol] = {v , head[u]}; head[u] = tol; } void init(){ ME(head , 0); ME(fa , 0); ME(de , 0); tol = 0; rep(i , 1 , n){ f[i] = i ; } } void dfs(int u , int pre){ de[u] = de[pre]+1; fa[u][0] = pre; for(int i = 1 ; (1<<i) <= de[u] ; i++){ fa[u][i] = fa[fa[u][i-1]][i-1]; } for(int i = head[u] ; i ; i = g[i].next){ int v = g[i].to; if(v == pre) continue; dfs(v , u); } } int lca(int u , int v){ if(de[u] < de[v]) swap(u , v); red(i , 20 , 0){ if(de[u] - (1<<i) >= de[v]){ u = fa[u][i]; } } if(u == v) return v ; red(i , 20 , 0){ if(fa[u][i] != fa[v][i]){ u = fa[u][i]; v = fa[v][i]; } } return fa[u][0]; } int get_dis(int u , int v){ return de[u] + de[v] - 2 * de[lca(u , v)]; } void solve(){ cin >> n ; init(); rep(i , 1 , n-1){ int u , v; cin >> u >> v ; add(u , v); add(v , u); } int m ;cin >> m; dfs(1 , 0); rep(i , 1 , m){ int u , v , a , b , w; cin >> a >> b >> u >> v >> w; int dis1 = get_dis(u , v); int dis2 = get_dis(u , a)+1+get_dis(b , v); int dis3 = get_dis(u , b)+1+get_dis(a , v); if((dis1 <= w && dis1%2==w%2) ||(dis2 <= w && dis2%2==w%2) || (dis3 <= w && dis3%2==w%2)){ cout << "YES" << endl; }else{ cout << "NO" << endl; } } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); //int t ; //cin >> t ; //while(t--){ solve(); //} }