Codeforcesラウンド#617(DIV。3)F.バーランドビューティー

トピックリンク
質問の意味:あなたの木といくつかの制限を与えるので、あなたが制限を満たすために、各辺の右側を構築します。
制限事項: A - > B A-> B 最小辺の重みは、最短経路に等しいです C C

思考:すべては、各パスの割り当てに関するすべての制限を介して順次次に大きい順に制限する、およびc電流cに割り当てられていません。あなたは値と値を持っている場合はcより大きくない場合、明らかに何の解決策。
参照は、コードの詳細を示します。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
#define fi first
#define se second
#define pb push_back
int n,m;
vector<int>v[N];
int ans[N];
struct uzi{
  int a,b,c;
  bool operator <(const uzi & t)const{
    return c>t.c;
  }
}p[N];
int pr[N];
int ed[5001][5001];
int dfs(int now,int d,int pre){
  pr[now]=pre;
  if(now==d){
    return 1;
  }
 // cout<<now<<' '<<pre<<endl;
  for(auto k:v[now]){
    if(k==pre)
      continue;
    if(dfs(k,d,now))return 1;
  }
  return 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin>>n;
  for(int i=1;i<n;i++){
    int s,t;
    cin>>s>>t;
    ed[s][t]=ed[t][s]=i;
    v[s].pb(t);
    v[t].pb(s);
    ans[i]=0;
  }
  cin>>m;
  for(int i=1;i<=m;i++){
    int l,r,w;
    cin>>l>>r>>w;
    p[i]={l,r,w};
  }
  sort(p+1,p+1+m);
  for(int i=1;i<=m;i++){
    dfs(p[i].a,p[i].b,0);
    int sta=0;
    while(pr[p[i].b]){
      int x=p[i].b,y=pr[p[i].b];
      x=ed[x][y];
      if(ans[x]<=p[i].c){
        sta=1;
      }
      ans[x]=max(ans[x],p[i].c);
      p[i].b=pr[p[i].b];
    }
    if(!sta)return cout<<-1,0;
  }
  for(int i=1;i<n;i++){
    if(!ans[i])ans[i]=1e6;
    cout<<ans[i]<< ' ';
  }
  return 0;
}
公開された158元の記事 ウォン称賛81 ビュー9646

おすすめ

転載: blog.csdn.net/qq_40655981/article/details/104355191