【ABC320D】题解

Relative Position 题解

题目保证不矛盾,就可以直接 vector 建图,然后 dfs 一遍,边权为 (wx,wy) 表示坐标的差,从 u=1 开始搜索,设点 u,v 有一条无向边,vx=ux+wx,vy=uy+wy,最后如果没有被标记过的话(也就是没有遍历过),那么无解。

AC Code

#include<iostream>
#include<vector>

using namespace std;

const int MAXN = 2e5 + 10;

struct node{
    int t, x, y;
};

struct ANS{
    long long a, b;
}ans[MAXN];

vector<node> G[MAXN];
int n, m, a, b, x, y;
bool vis[MAXN];

void dfs(int u, long long x, long long y){
    if (vis[u]){
        return;
    }
    vis[u] = 1,ans[u] = {x, y};
    for (int i = 0; i < G[u].size(); i++){
        dfs(G[u][i].t, x + G[u][i].x, y + G[u][i].y);
    }
}

int main(){
    cin >> n >> m;
    for (int i = 1; i <= m; i++){
        cin >> a >> b >> x >> y;
        G[a].push_back({b, x, y}), G[b].push_back({a, -x, -y});
    }
    dfs(1, 0, 0);
    for (int i = 1; i <= n; i++){
        if (!vis[i]){
            cout << "undecidable\n";
        }
        else {
            cout << ans[i].a << ' ' << ans[i].b << '\n';
        }
    }
    return 0;
}
posted @   rksm2333  阅读(9)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示