結果

問題 No.2277 Honest or Dishonest ?
ユーザー 👑 eoeoeoeo
提出日時 2023-04-22 03:07:38
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 10,747 bytes
コンパイル時間 3,846 ms
コンパイル使用メモリ 269,096 KB
実行使用メモリ 19,912 KB
最終ジャッジ日時 2024-11-08 06:49:24
合計ジャッジ時間 7,782 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 64 ms
15,232 KB
testcase_04 AC 78 ms
17,464 KB
testcase_05 AC 35 ms
11,804 KB
testcase_06 AC 50 ms
12,672 KB
testcase_07 AC 43 ms
11,648 KB
testcase_08 AC 31 ms
12,180 KB
testcase_09 AC 50 ms
13,636 KB
testcase_10 AC 30 ms
9,088 KB
testcase_11 AC 15 ms
6,144 KB
testcase_12 AC 21 ms
7,680 KB
testcase_13 AC 57 ms
14,080 KB
testcase_14 AC 25 ms
9,344 KB
testcase_15 AC 27 ms
10,052 KB
testcase_16 AC 42 ms
11,520 KB
testcase_17 AC 21 ms
7,296 KB
testcase_18 AC 71 ms
16,888 KB
testcase_19 AC 71 ms
16,940 KB
testcase_20 AC 54 ms
13,728 KB
testcase_21 AC 37 ms
10,624 KB
testcase_22 AC 48 ms
12,256 KB
testcase_23 AC 50 ms
13,056 KB
testcase_24 AC 42 ms
11,776 KB
testcase_25 AC 57 ms
15,124 KB
testcase_26 AC 24 ms
10,832 KB
testcase_27 AC 25 ms
8,192 KB
testcase_28 AC 28 ms
9,852 KB
testcase_29 AC 42 ms
12,824 KB
testcase_30 AC 26 ms
8,576 KB
testcase_31 AC 47 ms
12,544 KB
testcase_32 AC 37 ms
12,052 KB
testcase_33 AC 70 ms
16,400 KB
testcase_34 AC 64 ms
16,100 KB
testcase_35 AC 14 ms
7,560 KB
testcase_36 AC 39 ms
11,388 KB
testcase_37 AC 55 ms
14,080 KB
testcase_38 AC 12 ms
5,632 KB
testcase_39 AC 17 ms
6,912 KB
testcase_40 AC 13 ms
6,784 KB
testcase_41 AC 70 ms
16,768 KB
testcase_42 AC 49 ms
13,056 KB
testcase_43 AC 90 ms
19,904 KB
testcase_44 AC 91 ms
19,904 KB
testcase_45 AC 89 ms
19,912 KB
testcase_46 AC 90 ms
19,904 KB
testcase_47 AC 90 ms
19,912 KB
testcase_48 AC 91 ms
19,896 KB
testcase_49 AC 91 ms
19,892 KB
testcase_50 AC 93 ms
19,904 KB
testcase_51 AC 92 ms
19,872 KB
testcase_52 AC 91 ms
19,772 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'void solve(int)':
main.cpp:98:28: warning: ignoring return value of 'constexpr std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = long long int; _Alloc = std::allocator<long long int>; reference = long long int&; size_type = long unsigned int]', declared with attribute 'nodiscard' [-Wunused-result]
   98 |         a[i]--, b[i]--, c[i];
      |                            ^
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/vector:64,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/functional:62,
                 from /home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/x86_64-pc-linux-gnu/bits/stdc++.h:71,
                 from main.cpp:1:
/home/linuxbrew/.linuxbrew/Cellar/gcc@12/12.3.0/include/c++/12/bits/stl_vector.h:1121:7: note: declared here
 1121 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |       ^~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef MY_DEBUG
#define dbg(...) debug_out(__VA_ARGS__)
#define dbgn(var) debug_out(#var, "=", var)
#else
#define dbg(...)
#define dbgn(...)
#endif
void debug_out();
template <class Head, class... Tail> void debug_out(Head, Tail...);
template <class T> bool chmax(T&, const T&);
template <class T> bool chmin(T&, const T&);
template <class T> T pwr(T, ll);
template <class T> T squ(T x) { return x * x; }
ll fact(ll);
ll comb(ll, ll);
ll ctoll(char);
char lltoc(ll);
bool flg(ll b, ll i) { return ((b) >> (i)) & 1LL; }  // flg(0b010, 1LL) -> true
const ll LINF = (ll)1e18 + 7;
const double EPS = 1e-9;
const int MAX_DUBUG_SIZE = 200;

#include <atcoder/math>
#include <atcoder/modint>
using namespace atcoder;

const int MOD = 998244353;
using mint = modint998244353;

ostream& operator<<(ostream& os, const mint& N) { return os << N.val(); }

struct edge {
    ll to;
    ll w;
    // edge v;
    // v.to, v.w でメンバ変数を呼び出す
    edge(ll to, ll w) : to(to), w(w) {}
    // graph g(n); で n 頂点のグラフを宣言
    // graph[from].push_back(edge(to, w)) の形で使う
};

using graph = vector<vector<edge>>;

//
// 参考記事: https://qiita.com/Morifolium/items/6c8f0a188af2f9620db2
//
// グラフ、頂点の入次数、頂点数を受け取り、そのトポロジカルソート(辞書式順序)を記録した配列を返す関数
// 頂点は 0 index, しがらみがなくなった頂点から queue に入れていく
vector<ll> topological_sort(vector<vector<ll>>& G, vector<ll>& indegree, ll V) {
    // トポロジカルソートを記録する配列
    vector<ll> sorted_vertices;

    //*********************************注意********************************
    // 埋める順を小さい順にするためpriority_queue を用いているが
    // 通常のトポロジカルソートはqueueでよい
    //*********************************************************************

    // 入次数が0の頂点を発見したら、処理待ち頂点としてキューに追加する
    priority_queue<ll, vector<ll>, greater<ll>> que;
    for (ll i = 0; i < V; i++) {
        if (indegree[i] == 0) {
            que.push(i);
        }
    }

    // キューが空になるまで、操作1~3を繰り返す
    while (que.empty() == false) {
        // キューの先頭の頂点を取り出す
        ll v = que.top();
        que.pop();

        // その頂点と隣接している頂点の入次数を減らし、0になればキューに追加
        for (ll i = 0; i < G[v].size(); i++) {
            ll u = G[v][i];
            indegree[u] -= 1;
            if (indegree[u] == 0) que.push(u);
        }
        // 頂点vを配列の末尾に追加する
        sorted_vertices.push_back(v);
    }

    // トポロジカルソートを返す
    return sorted_vertices;
}

void solve([[maybe_unused]] int test) {
    ll n, q;
    cin >> n >> q;
    vector<ll> a(q), b(q), c(q);
    graph g(n);                // 0-index, 無向グラフに直し忘れない.
    vector<vector<ll>> g2(n);  // 0-index, 無向グラフに直し忘れない.
    vector<ll> indegree(n, 0);
    for (ll i = 0; i < q; i++) {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--, b[i]--, c[i];
        g[a[i]].push_back(edge(b[i], c[i]));
        g[b[i]].push_back(edge(a[i], c[i]));
        g2[a[i]].push_back(b[i]);
        indegree[b[i]]++;
    }  // Aren't they one line?

    auto sorted_vertices = topological_sort(g2, indegree, n);

    mint ans = 1;
    vector<ll> dist(n, 0);  // グラフの頂点数を合わせる.

        for (ll i = 0; i < n; i++) {
        queue<ll> qu;

        ll start_node = i;  // BFSの開始点を合わせる.

        if (dist[start_node] != 0) {
            continue;
        }

        qu.push(start_node);
        dist[start_node] = i + 1;

        ll cnt = 2;
        bool same = false;
        bool ne = false;
        while (!qu.empty()) {
            ll now_node = qu.front();
            qu.pop();

            for (auto&& [next_node, flg] : g[now_node]) {
                ll next_dist = 0;
                if ((flg == 0 and dist[now_node] > 0) or (flg == 1 and dist[now_node] < 0)) {
                    next_dist = dist[start_node];
                }
                else {
                    next_dist = -dist[start_node];
                }

                // dbg(next_dist, next_node, dist[next_node]);
                // dbg(dist);

                if (dist[next_node] != 0) {
                    if (same) {
                        if (next_dist * dist[next_node] < 0) {
                            ans = 0;
                            dbg(111);
                        }
                    }
                    if (ne) {
                        if (next_dist * dist[next_node] > 0) {
                            ans = 0;
                            dbg(111);
                        }
                    }

                    if (next_dist == -dist[next_node]) {
                        dbg(now_node, next_node, next_dist, dist[next_node]);
                        dbg(dist);
                        ans = 0;
                        continue;
                    }
                    else if (next_dist == dist[next_node]) {
                        continue;
                    }
                    else if (next_dist * dist[next_node] < 0) {
                        ne = true;
                    }
                    else if (next_dist * dist[next_node] > 0) {
                        same = true;
                    }
                    cnt = 1;
                    continue;
                }

                dist[next_node] = next_dist;
                qu.push(next_node);
            }
        }

        ans *= cnt;
    }

    dbg(dist);

    if (ans == 1) {
        cout << 0 << "\n";
    }
    else {
        cout << ans << "\n";
    }
}

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(12);

    int testcase_size = 1;
    // cin >> testcase_size;

    for (int _t = 0; _t < testcase_size; _t++) {
        solve(_t);
    }
}

/*



























-----------------------------------MY_FUNCTIONS------------------------------------ */
template <class First, class Second> ostream& operator<<(ostream& os, const pair<First, Second>& pp) {
    return os << "{" << pp.first << "," << pp.second << "}";
}

template <class T> ostream& operator<<(ostream& os, const vector<T>& V) {
    if (V.empty()) return os << "[]";
    os << "[";
    for (ll i = 0; i < (ll)V.size(); i++) {
        os << V[i] << (i == int(V.size() - 1) ? "]" : ",");
    }
    return os;
}

template <class T> ostream& operator<<(ostream& os, const vector<vector<T>>& VV) {
    if (VV.empty()) return os << "[[]]";
    os << "[\n";
    for (auto&& V : VV) {
        os << V << "\n";
    }
    os << "]";
    return os;
}

template <class T> ostream& operator<<(ostream& os, const vector<vector<vector<T>>>& VVV) {
    if (VVV.empty()) return os << "[[[]]]";
    os << "["
       << "\n";
    int cnt = 0;
    for (auto&& VV : VVV) {
        os << cnt++ << VV << "\n\n";
    }
    os << "]";
    return os;
}

template <class T> ostream& operator<<(ostream& os, const set<T>& SS) {
    if (SS.empty()) return os << "[]";
    os << "[";
    auto ii = SS.begin();
    for (; ii != SS.end(); ii++) os << *ii << (ii == prev(SS.end()) ? "]" : ",");
    return os;
}

template <class Key, class Tp> ostream& operator<<(ostream& os, const map<Key, Tp>& MM) {
    if (MM.empty()) return os << "[{:}]";
    os << "[";
    auto ii = MM.begin();
    for (; ii != MM.end(); ii++)
        os << "{" << ii->first << ":" << ii->second << "}" << (ii == prev(MM.end()) ? "]" : ",");
    return os;
}

void debug_out() { cerr << endl; }

void debug_out_vl(vector<ll> V) {
    const int MAX_SIZE = min((int)V.size(), MAX_DUBUG_SIZE);

    cerr << "\033[33m";
    if (V.empty()) {
        cerr << "[]" << endl;
        return;
    }
    cerr << "[";
    for (int i = 0; i < MAX_SIZE; i++) {
        if (V[i] == LINF)
            cerr << "INF";
        else
            cerr << V[i];

        if (i == (int)V.size() - 1)
            cerr << "]\n";
        else if (i == MAX_DUBUG_SIZE - 1)
            cerr << ",...\n";
        else
            cerr << ",";
    }
    return;
}

void debug_out_vvl(vector<vector<ll>> VV) {
    cerr << "\033[33m";
    if (VV.empty()) {
        cerr << "[[]]" << endl;
        return;
    }
    cerr << "[\n";

    int MAX_ROW = min((int)VV.size(), MAX_DUBUG_SIZE);
    for (int i = 0; i < MAX_ROW; i++) {
        const int MAX_COLUMN = min((int)VV[i].size(), MAX_DUBUG_SIZE);
        if (VV[i].empty()) {
            cerr << "[]" << endl;
            continue;
        }
        cerr << "[";
        for (int j = 0; j < MAX_COLUMN; j++) {
            if (VV[i][j] == LINF)
                cerr << "INF";
            else
                cerr << VV[i][j];

            if (j == (int)VV[i].size() - 1)
                cerr << "]\n";
            else if (j == MAX_DUBUG_SIZE - 1)
                cerr << ",...\n";
            else
                cerr << ",";
        }

        if (i != (int)VV.size() - 1 and i == MAX_DUBUG_SIZE - 1) {
            cerr << ":\n:\033[m\n";
            return;
        }
    }

    cerr << "]\033[m\n";
    return;
}

template <class Head, class... Tail> void debug_out(Head H, Tail... T) {
    if constexpr (std::is_same_v<Head, vector<ll>>) {
        debug_out_vl(H);
    }
    else if constexpr (std::is_same_v<Head, vector<vector<ll>>>) {
        debug_out_vvl(H);
    }
    else {
        cerr << "\033[33m" << H << "\033[m ";
    }

    debug_out(T...);
}

template <class T> bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T> bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template <class T> T pwr(T x, ll n) {
    T res = 1;
    for (int i = 0; i < n; i++) {
        res *= x;
    }
    return res;
}

ll fact(ll n) {
    ll res = 1;
    for (ll i = 1; i <= n; i++) res *= i;
    return res;
}

ll comb(ll n, ll r) {  // comb(60, 30)までオーバーフローなし
    ll res = 1;
    for (int i = 1; i <= r; i++) {
        res *= n--;
        res /= i;
    }
    return res;
}

ll ctoll(char c) {
    if (c < '0' or '9' < c) {
        cerr << "\n\033[33m ctoll に '0'~'9' 以外の文字が入りました.\033[m\n" << endl;
    }
    return ll(c - '0');
}

char lltoc(ll n) {
    if (n < 0 or 9 < n) {
        cerr << "\n\033[33m lltoc に 0 ~ 9 以外の数字が入りました.\033[m\n" << endl;
    }
    return char(n + '0');
}
0