224redfg

cpp 复制代码
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<unordered_map>
#define int long long
using namespace std;
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        //cout << "---------------------------------\n";
        int n;
        cin >> n;
        vector<vector<int>>g(n);
        vector<int>in(n);
        for (int i = 0;i < n-1;i++)
        {
            int u, v, x, y;
            cin >> u >> v >> x >> y;
            v--;u--;
            if (x > y)
            {
                g[v].push_back(u);
                in[u]++;
            }
            else
            {
                g[u].push_back(v);
                in[v]++;
            }
        }
        vector<int>seq;
        queue<int>q;
        for (int i = 0;i < n;i++)
        {
            if (in[i] == 0)
                q.push(i);
        }
        while (!q.empty())
        {
            int u = q.front();
            seq.push_back(u);
            q.pop();
            for (auto v : g[u])
            {
                in[v]--;
                if (in[v] == 0)
                    q.push(v);
            }
        }
        
        vector<int>ans(n);
        for (int i = 0;i < n;i++)
        {
            ans[seq[i]] = i + 1;
        }
        for (auto e : ans)
            cout << e << " ";
        cout << "\n";
    }
    return 0;
}
相关推荐
老花眼猫3 小时前
C语言绘图方法:(10)椭圆缩放旋转
c语言·经验分享·青少年编程·课程设计
Zwawa1 天前
陀螺仪数据为什么看起来总在动:零偏、噪声和零漂
c语言·单片机·嵌入式
soso19681 天前
低功耗设计与BMS开发指南
c语言·开发语言·汇编
老师我太想进步了20261 天前
【数据结构-8】栈的实现
c语言·数据结构·算法
LCG元1 天前
串口乱码,先算波特率误差再动代码
c语言·嵌入式
another heaven2 天前
【C/C++ 第一次跨越(上):用 C++11/14 停止制造新的旧代码】
c语言·c++·制造
another heaven3 天前
【C/C++ Ranges 是零开销抽象吗?拆开管道、惰性求值和编译期膨胀】
c语言·开发语言·c++
Code_Solitude3 天前
C语言动态内存 + 柔性数组 + 数组名退化
c语言·算法·柔性数组
Zwawa3 天前
让屏幕动起来:OLED动画效果,丝滑光标、帧动画和一只贪吃蛇
c语言·单片机·嵌入式
꧁细听勿语情꧂3 天前
直接选择排序,堆排序,冒泡排序,递归快速排序、hoare版本、挖坑法、lomuto前后指针,非递归快速排序
c语言·开发语言·数据结构·算法