Codeforces 1296F Berland Beauty

Codeforces 1296F Berland Beauty

传送门:https://codeforces.com/contest/1296/problem/F

题意:

给你一个n个点的树,树有边权

现在有m个限制条件,表示点u到点v路径上的最小值是w

现在让我们构造这个树的边权,如果不能构造输出-1

题解:

因为n只有5000,所以对于每一个限制条件,我们都在路径上保存一下这条边的最大边权,记录一下这个限制条件中路径上经过的边的编号

最后再check一遍m次限制路径上的边权的最小值是否等于w即可

代码:

/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
// warm heart, wagging tail,and a smile just for you!
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                     佛祖保佑      永无BUG
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <bitset>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double Pi = acos(-1);
LL gcd(LL a, LL b) {
    return b ? gcd(b, a % b) : a;
}
LL lcm(LL a, LL b) {
    return a / gcd(a, b) * b;
}
double dpow(double a, LL b) {
    double ans = 1.0;
    while(b) {
        if(b % 2)ans = ans * a;
        a = a * a;
        b /= 2;
    } return ans;
}
LL quick_pow(LL x, LL y) {
    LL ans = 1;
    while(y) {
        if(y & 1) {
            ans = ans * x % mod;
        } x = x * x % mod;
        y >>= 1;
    } return ans;
}
struct EDGE {
    int v, nxt, w;
} edge[maxn << 1];
int head[maxn], tot;
void init() {
    memset(head, -1, sizeof(head));
    tot = 0;
}
void add_edge(int u, int v, int w) {
    edge[tot].v = v;
    edge[tot].w = w;
    edge[tot].nxt = head[u];
    head[u] = tot++;
}
int to[maxn];
int lim[maxn];
int ans[maxn];
vector<int> vec[maxn];
bool dfs(int u, int fa, int  id) {
    if(u == to[id]) return true;
    for(int i = head[u]; i != -1; i = edge[i].nxt) {
        int v = edge[i].v;
        if(v == fa) continue;
        if(dfs(v, u, id)) {
            int val = edge[i].w;//第几条边,保存一下最大值
            ans[val] = max(ans[val], lim[id]);
            vec[id].push_back(val);
            return true;
        }
    }
    return false;
}
int main() {
#ifndef ONLINE_JUDGE
    FIN
#endif
    int n;
    init();
    scanf("%d", &n);
    for(int i = 1, u, v; i < n; i++) {
        scanf("%d%d", &u, &v);
        add_edge(u, v, i);
        add_edge(v, u, i);
    }
    int m;
    scanf("%d", &m);
    for(int i = 1; i <= m; i++) {
        int u, v, w;
        scanf("%d%d%d", &u, &v, &w);
        to[i] = v;
        lim[i] = w;
        dfs(u, 0, i);
    }
    int flag = 1;
    for(int i = 1; i <= m; i++) {
        int Minn = INF;
        for(auto it : vec[i]) {
            Minn = min(Minn, ans[it]);
        }
        if(Minn != lim[i]) {
            flag = 0;
            break;
        }
    }
    if(flag) {
        for(int i = 1; i < n; i++) {
            printf("%d%c", max(1, ans[i]), i == n - 1 ? '\n' : ' ');
        }
    } else {
        printf("-1\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/buerdepepeqi/p/12263371.html