96802024-02-24 17:35:52szilKutyavetélkedő 2cpp17Elfogadva 100/100504ms113940 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define MAX(a, b) a = max(a, b)

const int MAXN = 250001;
const int MAXK = 250001;

int n, k, m;
vector<int> g[MAXK], gr[MAXK], gc[MAXK];
// bitset<MAXK> bs[MAXK];
set<pair<int, int>> edges;
int t[MAXN], scc[MAXK], cnt[MAXK], timer = 0;
bool vis[MAXK];
stack<int> order;
bool seen4[MAXK];

void dfs(int u) {
    vis[u] = 1;
    for (int v : g[u]) {
        if (!vis[v]) dfs(v);
    }
    order.push(u);
}

void dfs2(int u) {
    scc[u] = timer;
    for (int v : gr[u]) {
        if (!scc[v]) dfs2(v);
    }
}

// void dfs3(int u) {
//     bs[u][u] = 1;
//     for (int v : gc[u]) {
//         if (bs[v].none()) {
//             dfs3(v);
//         }
//         bs[u] |= bs[v];
//     }
// }

bool dfs4(int x, int keres){
    if(x == keres)return true;
    if(seen4[x]) return false;
    seen4[x] = true;
    for(int i : gc[x]){
        if(dfs4(i, keres)){
            return true;
        }
    }
    return false;
}

// bool Q(int u, int v) {
//     return bs[scc[u]][scc[v]];
// }

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> k;
    for (int i = 1; i <= n; i++) cin >> t[i];
    cin >> m;
    for (int i = 0; i < m; i++) {
        int a, b; cin >> a >> b;
        edges.insert({a, b});
        g[a].emplace_back(b);
        gr[b].emplace_back(a);
    }
    
    for (int i = 1; i <= k; i++) {
        if (!vis[i]) dfs(i);
    }
    
    while (!order.empty()) {
        int u = order.top();
        order.pop();
        if (scc[u]) continue;
        timer++;
        dfs2(u);
    }
    
    for(int i = 1; i <= k; i++) {
        for (int v : g[i]) {
            if (scc[i] != scc[v]) {
                gc[scc[i]].emplace_back(scc[v]);
            }
        }
    }

    for (int i = 1; i <= timer; i++) {
        sort(gc[i].begin(), gc[i].end());
        gc[i].erase(unique(gc[i].begin(), gc[i].end()), gc[i].end()); 
    }
    int last = n+1;
    for (int i = 1; i <= n; i++) {
        if(i == 1 || scc[t[i]] != scc[t[i-1]]) cnt[scc[t[i]]]++;
        if (cnt[scc[t[i]]] == 2){
            last = i-1;
            break;
        }
    }

    // int last = 0;
    // for (int i = 1; i <= timer; i++) {
    //     if (first[i] != 1e6) last = max(last, first[i]);
    // }

    // for (last++; last <= n; last++) {
    //     if (scc[t[last]] != scc[t[last-1]]) {
    //         last--;
    //         break;
    //     }
    // }



    // for (int i = 1; i <= timer; i++) {
    //     if (bs[i].none()) dfs3(i);
    // }
    last = min(last, n);
    int utolsojo = last;
    for(int i = last-1; i >= 1; i--){
        if(i < n && scc[t[i]] == scc[t[i+1]]) continue;
        if (i == n) {
            // dfs4(i, -1);
            continue;
        }
        seen4[scc[t[i]]] = 0;
        if(!dfs4(scc[t[i]], scc[t[i+1]])){
            utolsojo = i;
        }
    }

    utolsojo = min(utolsojo, n);


    int ans = 2;
    for(int i = 2; i <= utolsojo; i++){
        if(edges.count({t[i-1], t[i]}) || t[i-1] == t[i]){
            ans+=2;
        } else {
            ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}
RészfeladatÖsszpontTesztVerdiktIdőMemória
subtask10/0
1Elfogadva19ms37096 KiB
2Elfogadva17ms37340 KiB
subtask221/21
3Elfogadva16ms37512 KiB
4Elfogadva14ms37624 KiB
5Elfogadva16ms37700 KiB
6Elfogadva16ms37840 KiB
7Elfogadva16ms38200 KiB
8Elfogadva16ms38256 KiB
9Elfogadva16ms38164 KiB
subtask345/45
10Elfogadva143ms54132 KiB
11Elfogadva130ms54048 KiB
12Elfogadva173ms60360 KiB
13Elfogadva170ms60136 KiB
14Elfogadva175ms60044 KiB
15Elfogadva165ms60444 KiB
16Elfogadva184ms61440 KiB
17Elfogadva181ms64452 KiB
18Elfogadva192ms64340 KiB
19Elfogadva187ms66892 KiB
20Elfogadva172ms67092 KiB
subtask434/34
21Elfogadva428ms84944 KiB
22Elfogadva474ms94760 KiB
23Elfogadva501ms109132 KiB
24Elfogadva423ms113940 KiB
25Elfogadva504ms105980 KiB
26Elfogadva467ms96584 KiB