前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >洛谷P4578 [FJOI2018]所罗门王的宝藏(dfs)

洛谷P4578 [FJOI2018]所罗门王的宝藏(dfs)

作者头像
attack
发布2019-03-11 14:22:22
4010
发布2019-03-11 14:22:22
举报

题意

题目链接

Sol

对于每个询问x, y, c

从在(x, y)之间连一条边权为c的双向边,然后就是解K个二元方程。

随便带个数进去找找环就行了

代码语言:javascript
复制
#include<bits/stdc++.h>
#define LL long long 
#define fi first
#define se second
#define Pair pair<int, int> 
#define Fin(x) freopen(#x".in", "r", stdin);
using namespace std;
const int MAXN = 3001, INF = 1e9 + 10;
template<typename A, typename B> inline bool chmax(A &x, B y) {return x < y ? x = y, 1 : 0;}
template<typename A, typename B> inline bool chmin(A &x, B y) {return x > y ? x = y, 1 : 0;}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, M, val[MAXN], vis[MAXN];
vector<Pair> v[MAXN];
int dfs(int x) {
    vis[x] = 1;
    for(auto &to : v[x]) {
        if(vis[to.fi] && val[x] + val[to.fi] != to.se) return 0;
        else if(vis[to.fi]) continue;
        val[to.fi] = to.se - val[x];
        if(!dfs(to.fi)) return 0;
    }
    return 1;
}
void solve() {
    memset(val, 0, sizeof(val));
    memset(vis, 0, sizeof(vis));
    int N = read(), M = read(), K = read();
    for(int i = 1; i <= N + M; i++) v[i].clear();
    for(int i = 1; i <= K; i++) {
        int x = read(), y = read(), c = read();
        v[x].push_back({y + N, c});
        v[y + N].push_back({x, c});
    }
    for(int i = 1; i <= N + M; i++) 
        if(!vis[i] && !dfs(i)) 
            {puts("No"); return ;}
    puts("Yes");
}
signed main() {
//  Fin(a);
    for(int T = read(); T--; solve());
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-02-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • Sol
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档