結果

問題 No.1364 [Renaming] Road to Cherry from Zelkova
ユーザー ningenMeningenMe
提出日時 2021-01-23 00:10:45
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 650 ms / 2,500 ms
コード長 9,492 bytes
コンパイル時間 2,840 ms
コンパイル使用メモリ 220,364 KB
最終ジャッジ日時 2025-01-18 06:49:01
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

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);
}
//xflg_xyflg_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=12sat
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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0