AtCoder Beginner Contest 014

Submission #6902132

Source codeソースコード

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define chmax(a, b) a = (a >= b ? a : b)
#define chmin(a, b) a = (a <= b ? a : b)
#define out(a) cout << a << endl
#define outa(a, n) { rep(_, n) cout << a[_] << " "; cout << endl; }
#define outp(a, n) { cout << endl; rep(_, n) cout << a[_].F << " " << a[_].S << endl; }
#define SZ(v) (int)v.size()
#define inf (int)(1e9+7)
#define abs(x) (x >= 0 ? x : -(x))
#define ceil(a, b) a / b + !!(a % b)
#define FIX(a) fixed << setprecision(a)
#define LB(v, n) (int)(lower_bound(all(v), n) - v.begin())
#define UB(v, n) (int)(upper_bound(all(v), n) - v.begin())
ll pow(ll a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
ll nck(int n, int k) { ll ret = 1; rep(i, k) {ret *= n - i; ret /= i + 1; } return ret; } // n <= 60
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }





int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    
    
    int n;
    cin >> n;
    
    vector<int> v[n];
    rep(i, n - 1) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        v[a].eb(b);
        v[b].eb(a);
    }
    
    int reached[n];
    fill_n(reached, n, inf);
    reached[0] = 0;
    
    int lca[n];
    lca[0] = 0;
    
    queue<int> q;
    q.push(0);
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        
        for (auto nxt : v[now]) {
            if (reached[nxt] != inf) continue;
            reached[nxt] = reached[now] + 1;
            lca[nxt] = now;
            q.push(nxt);
        }
        
    }
    
    // outa(lca, n);
    
    
    
    
    int qu;
    cin >> qu;
    
    rep(i, qu) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        
        vector<int> anc;
        int now = a;
        
        while (1) {
            anc.eb(now);
            if (now == 0) break;
            now = lca[now];
        }
        
        // outa(anc, SZ(anc));
        
        now = b;
        int ans = -1;
        while (1) {
            rep(j, SZ(anc)) {
                if (anc[j] == now) {
                    ans = now;
                    break;
                }
            }
            if (ans != -1) break;
            now = lca[now];
        }
        
        out(reached[a] + reached[b] + 1 - 2 * reached[ans]);
    }
    
    
}


Submission

Task問題 D - 閉路
User nameユーザ名 tac_
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 TLE
Score得点 30
Source lengthソースコード長 2668 Byte
File nameファイル名
Exec time実行時間 ms
Memory usageメモリ使用量 -

Test case

Set

Set name Score得点 / Max score Cases
Sample - subtask0_sample01.txt,subtask0_sample02.txt,subtask0_sample03.txt
Subtask1 30 / 30 subtask1_01.txt,subtask1_02.txt,subtask1_03.txt,subtask1_04.txt,subtask1_05.txt,subtask1_06.txt,subtask1_07.txt,subtask1_08.txt,subtask1_09.txt,subtask1_10.txt,subtask1_11.txt,subtask1_12.txt
Subtask2 0 / 70 subtask0_sample01.txt,subtask0_sample02.txt,subtask0_sample03.txt,subtask1_01.txt,subtask1_02.txt,subtask1_03.txt,subtask1_04.txt,subtask1_05.txt,subtask1_06.txt,subtask1_07.txt,subtask1_08.txt,subtask1_09.txt,subtask1_10.txt,subtask1_11.txt,subtask1_12.txt,subtask2_01.txt,subtask2_02.txt,subtask2_03.txt,subtask2_04.txt,subtask2_05.txt,subtask2_06.txt,subtask2_07.txt,subtask2_08.txt,subtask2_09.txt,subtask2_10.txt,subtask2_11.txt,subtask2_12.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
subtask0_sample01.txt AC 1 ms 256 KB
subtask0_sample02.txt AC 1 ms 256 KB
subtask0_sample03.txt AC 1 ms 256 KB
subtask1_01.txt AC 29 ms 6528 KB
subtask1_02.txt AC 30 ms 6528 KB
subtask1_03.txt AC 1 ms 256 KB
subtask1_04.txt AC 1 ms 256 KB
subtask1_05.txt AC 1 ms 384 KB
subtask1_06.txt AC 1 ms 384 KB
subtask1_07.txt AC 40 ms 6656 KB
subtask1_08.txt AC 40 ms 6656 KB
subtask1_09.txt AC 40 ms 6528 KB
subtask1_10.txt AC 37 ms 6528 KB
subtask1_11.txt AC 40 ms 6528 KB
subtask1_12.txt AC 36 ms 6528 KB
subtask2_01.txt TLE
subtask2_02.txt TLE
subtask2_03.txt AC 186 ms 512 KB
subtask2_04.txt AC 205 ms 512 KB
subtask2_05.txt AC 379 ms 640 KB
subtask2_06.txt AC 746 ms 640 KB
subtask2_07.txt AC 286 ms 6912 KB
subtask2_08.txt AC 286 ms 6912 KB
subtask2_09.txt AC 751 ms 6912 KB
subtask2_10.txt TLE
subtask2_11.txt TLE
subtask2_12.txt TLE