6242 2023. 11. 08 13:07:32 CWM Őslényország cpp17 Időlimit túllépés 15/100 2.592s 319424 KiB
// KKGyak.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
using namespace std;

#include <iostream>
#include <map>
#include <set>
#include <unordered_set>
#include <vector>
#include <algorithm>

void DFS(vector<vector<int>>& graph, int beginnode, int num, vector<unordered_set<int>>& edgeCont) {
    edgeCont[beginnode].insert(num);
    for (auto g : graph[beginnode]) {
        DFS(graph, g, num, edgeCont);
    }
}

int main()
{
    int a;
    cin >> a;
    vector<vector<int>> g1(a+1);
    for (size_t i = 2; i < a+1; i++)
    {
        int b;
        cin >> b;
        g1[b].push_back(i);
    }
    int c;
    cin >> c;
    vector<vector<int>> g2(c + 1);
    for (size_t i = 2; i < c + 1; i++)
    {
        int b;
        cin >> b;
        g2[b].push_back(i);
    }
    int d;
    cin >> d;
    vector<unordered_set<int>> edgeCont1(a+1);
    vector<unordered_set<int>> edgeCont2(c+1);
    vector<int> weights(d);
    for (size_t i = 0; i < d; i++)
    {
        int v1, v2, w;
        cin >> v1 >> v2 >> w;
        weights[i] = w;
        DFS(g1, v1, i, edgeCont1);
        DFS(g2, v2, i, edgeCont2);
    }
    int q;
    cin >> q;
    for (size_t i = 0; i < q; i++)
    {
        int q1, q2;
        cin >> q1 >> q2;
        long long res = 0;
        for (auto query : edgeCont2[q2]) {
            if (edgeCont1[q1].count(query) == 1) {
                res += weights[query];
            }
        }
        cout << res << "\n";
    }
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Részfeladat Összpont Teszt Verdikt Idő Memória
subtask1 0/0
1 Elfogadva 3ms 1812 KiB
subtask2 15/15
2 Elfogadva 8ms 3024 KiB
3 Elfogadva 9ms 3460 KiB
4 Elfogadva 8ms 4360 KiB
5 Elfogadva 8ms 4412 KiB
6 Elfogadva 97ms 30708 KiB
7 Elfogadva 256ms 59684 KiB
8 Elfogadva 39ms 16088 KiB
9 Elfogadva 75ms 26208 KiB
subtask3 0/30
10 Időlimit túllépés 2.576s 217664 KiB
11 Időlimit túllépés 2.549s 290700 KiB
12 Időlimit túllépés 2.585s 299544 KiB
13 Időlimit túllépés 2.589s 316492 KiB
14 Időlimit túllépés 2.578s 245380 KiB
15 Időlimit túllépés 2.589s 247304 KiB
subtask4 0/25
16 Időlimit túllépés 2.562s 311296 KiB
17 Időlimit túllépés 2.571s 235624 KiB
18 Időlimit túllépés 2.568s 243572 KiB
19 Időlimit túllépés 2.591s 224552 KiB
20 Időlimit túllépés 2.588s 244916 KiB
21 Időlimit túllépés 2.568s 245760 KiB
subtask5 0/30
22 Időlimit túllépés 2.578s 241772 KiB
23 Időlimit túllépés 2.562s 244848 KiB
24 Időlimit túllépés 2.592s 319424 KiB
25 Időlimit túllépés 2.575s 317256 KiB
26 Időlimit túllépés 2.588s 310836 KiB