提出 #73718955


ソースコード 拡げる

#include <iostream>
#include <vector>
#include <stack>
#include <tuple>
#include <algorithm>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int Q;
    cin >> Q;
    while(Q--) 
    {
        int N;
        cin >> N;
        vector<vector<int>> adj(N + 1);
        vector<int> deg(N + 1, 0);
        for (int i = 0;i < N - 1;i++) 
        {
            int A,B;
            cin >> A >> B;
            adj[A].push_back(B);
            adj[B].push_back(A);
            deg[A]++;
            deg[B]++;
        }
        vector<bool> is_valid(N + 1, false);
        for(int u = 1;u <= N;u++) 
            if(deg[u] >= 3) is_valid[u] = true;
        vector<bool> visited(N + 1, false);
        vector<int> dp(N + 1, 0); 
        int ans = 1; 
        for (int u = 1;u <= N;u++) 
        {
            if(is_valid[u] && !visited[u]) 
            {
                stack<tuple<int, int, bool>> st;
                st.emplace(u, -1, false);
                int current_max = 1;
                while (!st.empty()) 
                {
                    auto [curr,parent,processed] = st.top();
                    st.pop();
                    if (!processed) 
                    {
                        st.emplace(curr, parent, true);
                        visited[curr] = true;
                        for(auto it = adj[curr].rbegin(); it != adj[curr].rend();it++) 
                        {
                            int v = *it;
                            if(v != parent && is_valid[v])  st.emplace(v, curr, false);
                        }
                    } 
                    else 
                    {
                        int first = 0, second = 0;
                        for(int v : adj[curr]) 
                        {
                            if(v == parent || !is_valid[v]) continue;
                            int val;
                            if(deg[v] >= 4) 
                            {
                                val = dp[v] + 1;
                            } 
                            else 
                            {
                                val = 2;
                            }
                            if(val > first) 
                            {
                                second = first;
                                first = val;
                            } 
                            else if(val > second) 
                            {
                                second = val;
                            }
                        }
                        dp[curr] = max(1, first);
                        current_max = max(current_max, dp[curr]);
                        if(deg[curr] >= 4) 
                        {
                            int merge_len = first + second - 1;
                            current_max = max(current_max, merge_len);
                        }
                    }
                }
                ans = max(ans, current_max);
            }
        }
    	cout << ans << '\n';
    }
    return 0;
}

提出情報

提出日時
問題 F - Centipede Graph
ユーザ rgr2025
言語 C++23 (GCC 15.2.0)
得点 500
コード長 3164 Byte
結果 AC
実行時間 60 ms
メモリ 16676 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 500 / 500
結果
AC × 1
AC × 47
セット名 テストケース
Sample 00_sample_00.txt
All 00_sample_00.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 01_small_05.txt, 01_small_06.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 03_path_00.txt, 03_path_01.txt, 03_path_02.txt, 03_path_03.txt, 03_path_04.txt, 03_path_05.txt, 03_path_06.txt, 03_path_07.txt, 03_path_08.txt, 03_path_09.txt, 04_star_00.txt, 04_star_01.txt, 04_star_02.txt, 05_uni_00.txt, 05_uni_01.txt, 05_uni_02.txt, 05_uni_03.txt, 05_uni_04.txt, 05_uni_05.txt, 06_bi-ternary_00.txt, 06_bi-ternary_01.txt, 06_bi-ternary_02.txt, 06_bi-ternary_03.txt, 06_bi-ternary_04.txt, 06_bi-ternary_05.txt, 06_bi-ternary_06.txt, 06_bi-ternary_07.txt, 06_bi-ternary_08.txt, 06_bi-ternary_09.txt, 06_bi-ternary_10.txt, 06_bi-ternary_11.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 1 ms 3612 KiB
01_small_00.txt AC 23 ms 3520 KiB
01_small_01.txt AC 23 ms 3584 KiB
01_small_02.txt AC 23 ms 3568 KiB
01_small_03.txt AC 23 ms 3680 KiB
01_small_04.txt AC 23 ms 3544 KiB
01_small_05.txt AC 23 ms 3584 KiB
01_small_06.txt AC 14 ms 3592 KiB
02_random_00.txt AC 27 ms 3768 KiB
02_random_01.txt AC 27 ms 3668 KiB
02_random_02.txt AC 27 ms 3696 KiB
02_random_03.txt AC 56 ms 14492 KiB
02_random_04.txt AC 41 ms 9260 KiB
02_random_05.txt AC 46 ms 12300 KiB
02_random_06.txt AC 47 ms 10728 KiB
02_random_07.txt AC 40 ms 8952 KiB
03_path_00.txt AC 27 ms 3708 KiB
03_path_01.txt AC 27 ms 3740 KiB
03_path_02.txt AC 29 ms 4220 KiB
03_path_03.txt AC 29 ms 4288 KiB
03_path_04.txt AC 38 ms 7944 KiB
03_path_05.txt AC 38 ms 8436 KiB
03_path_06.txt AC 60 ms 16520 KiB
03_path_07.txt AC 60 ms 16604 KiB
03_path_08.txt AC 60 ms 16604 KiB
03_path_09.txt AC 59 ms 16676 KiB
04_star_00.txt AC 38 ms 16632 KiB
04_star_01.txt AC 25 ms 4380 KiB
04_star_02.txt AC 37 ms 9908 KiB
05_uni_00.txt AC 27 ms 3584 KiB
05_uni_01.txt AC 28 ms 4304 KiB
05_uni_02.txt AC 39 ms 8792 KiB
05_uni_03.txt AC 57 ms 15900 KiB
05_uni_04.txt AC 56 ms 15936 KiB
05_uni_05.txt AC 57 ms 16080 KiB
06_bi-ternary_00.txt AC 54 ms 16028 KiB
06_bi-ternary_01.txt AC 38 ms 8488 KiB
06_bi-ternary_02.txt AC 27 ms 3672 KiB
06_bi-ternary_03.txt AC 55 ms 16036 KiB
06_bi-ternary_04.txt AC 42 ms 8228 KiB
06_bi-ternary_05.txt AC 27 ms 3636 KiB
06_bi-ternary_06.txt AC 53 ms 16028 KiB
06_bi-ternary_07.txt AC 43 ms 8996 KiB
06_bi-ternary_08.txt AC 27 ms 3644 KiB
06_bi-ternary_09.txt AC 60 ms 15936 KiB
06_bi-ternary_10.txt AC 40 ms 7416 KiB
06_bi-ternary_11.txt AC 27 ms 3740 KiB