結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー ningenMeningenMe
提出日時 2021-01-23 00:10:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 327 ms / 2,500 ms
コード長 9,492 bytes
コンパイル時間 2,870 ms
コンパイル使用メモリ 230,164 KB
実行使用メモリ 48,016 KB
最終ジャッジ日時 2024-06-09 07:37:08
合計ジャッジ時間 10,410 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 13 ms
6,528 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 11 ms
5,376 KB
testcase_11 AC 11 ms
5,760 KB
testcase_12 AC 10 ms
5,376 KB
testcase_13 AC 188 ms
34,304 KB
testcase_14 AC 242 ms
30,720 KB
testcase_15 AC 251 ms
35,840 KB
testcase_16 AC 157 ms
24,576 KB
testcase_17 AC 94 ms
25,984 KB
testcase_18 AC 327 ms
46,720 KB
testcase_19 AC 321 ms
46,720 KB
testcase_20 AC 320 ms
46,720 KB
testcase_21 AC 312 ms
46,720 KB
testcase_22 AC 325 ms
46,848 KB
testcase_23 AC 45 ms
14,592 KB
testcase_24 AC 28 ms
7,808 KB
testcase_25 AC 114 ms
21,120 KB
testcase_26 AC 206 ms
28,416 KB
testcase_27 AC 118 ms
20,352 KB
testcase_28 AC 88 ms
16,640 KB
testcase_29 AC 111 ms
19,072 KB
testcase_30 AC 83 ms
17,024 KB
testcase_31 AC 63 ms
15,616 KB
testcase_32 AC 84 ms
17,152 KB
testcase_33 AC 193 ms
25,472 KB
testcase_34 AC 196 ms
27,776 KB
testcase_35 AC 285 ms
38,528 KB
testcase_36 AC 262 ms
34,688 KB
testcase_37 AC 72 ms
14,720 KB
testcase_38 AC 98 ms
17,408 KB
testcase_39 AC 96 ms
17,536 KB
testcase_40 AC 97 ms
17,024 KB
testcase_41 AC 98 ms
17,408 KB
testcase_42 AC 98 ms
17,152 KB
testcase_43 AC 108 ms
48,016 KB
testcase_44 AC 89 ms
36,924 KB
testcase_45 AC 78 ms
37,632 KB
testcase_46 AC 37 ms
27,904 KB
testcase_47 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using int128  = __int128_t;
using int64   = long long;
using int32   = int;
using uint128 = __uint128_t;
using uint64  = unsigned long long;
using uint32  = unsigned int;

#define ALL(obj) (obj).begin(),(obj).end()
template<class T> using priority_queue_reverse = priority_queue<T,vector<T>,greater<T>>;

constexpr int64 MOD = 1'000'000'000LL + 7; //'
constexpr int64 MOD2 = 998244353;
constexpr int64 HIGHINF = 1'000'000'000'000'000'000LL;
constexpr int64 LOWINF = 1'000'000'000'000'000LL; //'
constexpr long double PI = 3.1415926535897932384626433L;

template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);}
template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const deque<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max(a,b);}
template <class T> void chmin(T& a, const T b){a=min(a,b);}
vector<string> split(const string &str, const char delemiter) {vector<string> res;stringstream ss(str);string buffer; while( getline(ss, buffer, delemiter) ) res.push_back(buffer); return res;}
inline constexpr int msb(int x) {return x?31-__builtin_clz(x):-1;}
inline constexpr int64 ceil_div(const int64 a,const int64 b) {return (a+(b-1))/b;}// return ceil(a/b)
void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;}

/*
 * @title ModInt
 * @docs md/util/ModInt.md
 */
template<long long mod> class ModInt {
public:
    long long x;
    constexpr ModInt():x(0) {}
    constexpr ModInt(long long y) : x(y>=0?(y%mod): (mod - (-y)%mod)%mod) {}
    ModInt &operator+=(const ModInt &p) {if((x += p.x) >= mod) x -= mod;return *this;}
    ModInt &operator+=(const long long y) {ModInt p(y);if((x += p.x) >= mod) x -= mod;return *this;}
    ModInt &operator+=(const int y) {ModInt p(y);if((x += p.x) >= mod) x -= mod;return *this;}
    ModInt &operator-=(const ModInt &p) {if((x += mod - p.x) >= mod) x -= mod;return *this;}
    ModInt &operator-=(const long long y) {ModInt p(y);if((x += mod - p.x) >= mod) x -= mod;return *this;}
    ModInt &operator-=(const int y) {ModInt p(y);if((x += mod - p.x) >= mod) x -= mod;return *this;}
    ModInt &operator*=(const ModInt &p) {x = (x * p.x % mod);return *this;}
    ModInt &operator*=(const long long y) {ModInt p(y);x = (x * p.x % mod);return *this;}
    ModInt &operator*=(const int y) {ModInt p(y);x = (x * p.x % mod);return *this;}
    ModInt &operator^=(const ModInt &p) {x = (x ^ p.x) % mod;return *this;}
    ModInt &operator^=(const long long y) {ModInt p(y);x = (x ^ p.x) % mod;return *this;}
    ModInt &operator^=(const int y) {ModInt p(y);x = (x ^ p.x) % mod;return *this;}
    ModInt &operator/=(const ModInt &p) {*this *= p.inv();return *this;}
    ModInt &operator/=(const long long y) {ModInt p(y);*this *= p.inv();return *this;}
    ModInt &operator/=(const int y) {ModInt p(y);*this *= p.inv();return *this;}
    ModInt operator=(const int y) {ModInt p(y);*this = p;return *this;}
    ModInt operator=(const long long y) {ModInt p(y);*this = p;return *this;}
    ModInt operator-() const {return ModInt(-x); }
    ModInt operator++() {x++;if(x>=mod) x-=mod;return *this;}
    ModInt operator--() {x--;if(x<0) x+=mod;return *this;}
    ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
    ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
    ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
    ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
    ModInt operator^(const ModInt &p) const { return ModInt(*this) ^= p; }
    bool operator==(const ModInt &p) const { return x == p.x; }
    bool operator!=(const ModInt &p) const { return x != p.x; }
    ModInt inv() const {int a=x,b=mod,u=1,v=0,t;while(b > 0) {t = a / b;swap(a -= t * b, b);swap(u -= t * v, v);} return ModInt(u);}
    ModInt pow(long long n) const {ModInt ret(1), mul(x);for(;n > 0;mul *= mul,n >>= 1) if(n & 1) ret *= mul;return ret;}
    friend ostream &operator<<(ostream &os, const ModInt &p) {return os << p.x;}
    friend istream &operator>>(istream &is, ModInt &a) {long long t;is >> t;a = ModInt<mod>(t);return (is);}
};
using modint = ModInt<MOD>;

/*
 * @title StronglyConnectedComponents - 強連結成分分解
 * @docs md/graph/StronglyConnectedComponents.md
 */
class StronglyConnectedComponents{
	int num,is_2sat,half,max_id,cnt;
	vector<vector<int>> edge;
	vector<int> label,order,low;
    stack<size_t> st;
	inline int rev(int i) { return i < half ? i + half : i - half; }
    inline void dfs(int& from) {
        low[from]=order[from]=cnt++;
        st.push(from);
        for(int& to:edge[from]) {
            if(order[to]==-1) {
                dfs(to);
                low[from]=min(low[from],low[to]);
            }
            else {
                low[from]=min(low[from],order[to]);
            }
        }
        if (low[from] == order[from]) {
            while(st.size()) {
                int u = st.top();st.pop();
                order[u] = num;
                label[u] = max_id;
                if (u == from) break;
            }
            max_id++;
        }
    }
public:
	StronglyConnectedComponents(const int n, bool is_2sat=0):num(n),max_id(0),cnt(0),is_2sat(is_2sat){
		if(is_2sat) num<<=1;
		edge.resize(num);
		label.resize(num);
        order.resize(num,-1);
        low.resize(num);
        half=(num>>1);
	}
	inline int operator[](int idx) {
		return label[idx];
	}
	inline void make_edge(const int from,const int to) {
		edge[from].push_back(to);
	}
    //xがflg_xならばyがflg_y
	inline void make_condition(int x, bool flg_x, int y, bool flg_y) {
		if (!flg_x) x = rev(x);
		if (!flg_y) y = rev(y);
		make_edge(x, y);
		make_edge(rev(y), rev(x));
	}
    //is_2sat=1のときに、2satを満たすかを返却する
	inline bool solve(void) {
        for (int i = 0; i < num; i++) if (order[i] == -1) dfs(i);
        for (int& id:label) id = max_id-1-id;
		if(!is_2sat) return true;
		for (int i = 0; i < num; ++i) if (label[i] == label[rev(i)]) return false;
		return true;
	}
    vector<vector<int>> topological_sort(void) {
        vector<vector<int>> ret(max_id);
        for(int i=0;i<num;++i) ret[label[i]].push_back(i);
        return ret;
    }
	int is_true(int i) {
		return label[i] > label[rev(i)];
	}
	void print(void) {
		for(auto id:label) cout << id << " ";
		cout << endl;
	}
};

/**
 * @url 
 * @est
 */ 
int main() {
    cin.tie(0);ios::sync_with_stdio(false);
    int N,M; cin >> N >> M;
    using t = tuple<int,int64,int64>;
    StronglyConnectedComponents scc0(N+1),sccN(N+1);
    vector<vector<t>> edges0(N+1),edgesN(N+1);
    for(int i=0;i<M;++i) {
        int u,v,l,a; cin >> u >> v >> l >> a;
        scc0.make_edge(u,v);
        edges0[u].push_back(t{v,l,a});
        sccN.make_edge(v,u);
        edgesN[v].push_back(t{u,l,a});
    }
    scc0.solve();
    auto vv0 = scc0.topological_sort();
    vector<modint> dp0(N+1,0);
    vector<int> is_inf(N+1,0);
    int is_0 = 0;
    dp0[0]=1;
    for(auto v:vv0) {
        for(int e:v) if(e==0) is_0 = 1;
        if(!is_0) continue;
        for(int from:v) {
            if(v.size()!=1) is_inf[from]=1;
            for(auto ed:edges0[from]) {
                int to = get<0>(ed);
                int64 a = get<2>(ed);
                is_inf[to] |= is_inf[from];
                if(is_inf[to]) continue;
                dp0[to]  += dp0[from]*a;
            }
        }
    }
    corner(is_inf[N],"INF");

    sccN.solve();
    auto vvN = sccN.topological_sort();
    vector<modint> dpN(N+1,0);
    dpN[N]=1;
    modint ans = 0;
    for(auto v:vvN) {
        for(int from:v) {
            for(auto ed:edgesN[from]) {
                int to = get<0>(ed);
                int64 l = get<1>(ed);
                int64 a = get<2>(ed);
                dpN[to]  += dpN[from]*a;
                ans += dp0[to]*dpN[from]*a*l;
            }
        }
    }
    cout << ans << endl;
    return 0;
}
0