Submission #58476353


Source Code Expand

#ifdef NACHIA
#define _GLIBCXX_DEBUG
#else
#define NDEBUG
#endif
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<int(n); i++)
const i64 INF = 1001001001001001001;
template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; }
template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; }
#include <atcoder/modint>
using Modint = atcoder::static_modint<998244353>;
#include <atcoder/scc>
#include <atcoder/dsu>
#include <atcoder/maxflow>
using namespace std;

void testcase(){
    i64 N; cin >> N;
    vector<pair<int,int>> S(N);
    int Z = 26;
    rep(i,N){
        string s; cin >> s;
        S[i] = { s[0] - 'A', s[1] - 'A' };
    }
    auto scc = atcoder::scc_graph(Z);
    rep(i,N) scc.add_edge(S[i].first, S[i].second);
    auto sccs = scc.scc();
    int sccn = int(sccs.size());
    vector<int> scci(Z);
    rep(i,sccn){
        for(int v : sccs[i]) scci[v] = i;
    }
    auto dsu = atcoder::dsu(sccn);
    vector<int> have_edge(sccn);
    auto mf = atcoder::mf_graph<int>(sccn + 2);
    int mf_src = sccn;
    int mf_snk = sccn + 1;
    int ans = 0;
    for(auto [u,v] : S){
        u = scci[u];
        v = scci[v];
        if(u == v) have_edge[u] = 1;
        ans += 1;
        mf.add_edge(u, v, 10000);
        mf.add_edge(mf_src, v, 1);
        mf.add_edge(u, mf_snk, 1);
        dsu.merge(u,v);
    }
    rep(i,sccn) if(dsu.size(i) == 1 && have_edge[i]) ans++;
    ans -= mf.flow(mf_src, mf_snk);
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    testcase();
    return 0;
}

Submission Info

Submission Time
Task G - Only One Product Name
User Nachia
Language C++ 17 (gcc 12.2)
Score 600
Code Size 1729 Byte
Status AC
Exec Time 1 ms
Memory 3736 KiB

Compile Error

In file included from /opt/ac-library/atcoder/scc:1,
                 from Main.cpp:18:
/opt/ac-library/atcoder/scc.hpp: In member function ‘void atcoder::scc_graph::add_edge(int, int)’:
/opt/ac-library/atcoder/scc.hpp:18:13: warning: unused variable ‘n’ [-Wunused-variable]
   18 |         int n = internal.num_vertices();
      |             ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 71
Set Name Test Cases
Sample example_00.txt, example_01.txt, example_02.txt
All example_00.txt, example_01.txt, example_02.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, hand_12.txt, hand_13.txt, hand_14.txt, hand_15.txt, hand_16.txt, hand_17.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt, random_36.txt, random_37.txt, random_38.txt, random_39.txt, random_40.txt, random_41.txt, random_42.txt, random_43.txt, random_44.txt, random_45.txt, random_46.txt, random_47.txt, random_48.txt, random_49.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3448 KiB
example_01.txt AC 1 ms 3524 KiB
example_02.txt AC 1 ms 3424 KiB
hand_00.txt AC 1 ms 3488 KiB
hand_01.txt AC 1 ms 3552 KiB
hand_02.txt AC 1 ms 3388 KiB
hand_03.txt AC 1 ms 3584 KiB
hand_04.txt AC 1 ms 3584 KiB
hand_05.txt AC 1 ms 3496 KiB
hand_06.txt AC 1 ms 3644 KiB
hand_07.txt AC 1 ms 3536 KiB
hand_08.txt AC 1 ms 3536 KiB
hand_09.txt AC 1 ms 3444 KiB
hand_10.txt AC 1 ms 3528 KiB
hand_11.txt AC 1 ms 3448 KiB
hand_12.txt AC 1 ms 3532 KiB
hand_13.txt AC 1 ms 3524 KiB
hand_14.txt AC 1 ms 3536 KiB
hand_15.txt AC 1 ms 3428 KiB
hand_16.txt AC 1 ms 3592 KiB
hand_17.txt AC 1 ms 3508 KiB
random_00.txt AC 1 ms 3452 KiB
random_01.txt AC 1 ms 3380 KiB
random_02.txt AC 1 ms 3448 KiB
random_03.txt AC 1 ms 3516 KiB
random_04.txt AC 1 ms 3496 KiB
random_05.txt AC 1 ms 3520 KiB
random_06.txt AC 1 ms 3596 KiB
random_07.txt AC 1 ms 3392 KiB
random_08.txt AC 1 ms 3460 KiB
random_09.txt AC 1 ms 3656 KiB
random_10.txt AC 1 ms 3528 KiB
random_11.txt AC 1 ms 3468 KiB
random_12.txt AC 1 ms 3588 KiB
random_13.txt AC 1 ms 3516 KiB
random_14.txt AC 1 ms 3504 KiB
random_15.txt AC 1 ms 3456 KiB
random_16.txt AC 1 ms 3584 KiB
random_17.txt AC 1 ms 3472 KiB
random_18.txt AC 1 ms 3544 KiB
random_19.txt AC 1 ms 3456 KiB
random_20.txt AC 1 ms 3512 KiB
random_21.txt AC 1 ms 3572 KiB
random_22.txt AC 1 ms 3392 KiB
random_23.txt AC 1 ms 3528 KiB
random_24.txt AC 1 ms 3656 KiB
random_25.txt AC 1 ms 3540 KiB
random_26.txt AC 1 ms 3520 KiB
random_27.txt AC 1 ms 3560 KiB
random_28.txt AC 1 ms 3540 KiB
random_29.txt AC 1 ms 3584 KiB
random_30.txt AC 1 ms 3532 KiB
random_31.txt AC 1 ms 3716 KiB
random_32.txt AC 1 ms 3608 KiB
random_33.txt AC 1 ms 3592 KiB
random_34.txt AC 1 ms 3592 KiB
random_35.txt AC 1 ms 3492 KiB
random_36.txt AC 1 ms 3596 KiB
random_37.txt AC 1 ms 3496 KiB
random_38.txt AC 1 ms 3508 KiB
random_39.txt AC 1 ms 3560 KiB
random_40.txt AC 1 ms 3640 KiB
random_41.txt AC 1 ms 3456 KiB
random_42.txt AC 1 ms 3736 KiB
random_43.txt AC 1 ms 3568 KiB
random_44.txt AC 1 ms 3500 KiB
random_45.txt AC 1 ms 3656 KiB
random_46.txt AC 1 ms 3468 KiB
random_47.txt AC 1 ms 3532 KiB
random_48.txt AC 1 ms 3564 KiB
random_49.txt AC 1 ms 3516 KiB