結果
問題 |
No.1364 [Renaming] Road to Cherry from Zelkova
|
ユーザー |
![]() |
提出日時 | 2021-01-23 01:44:06 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 564 ms / 2,500 ms |
コード長 | 3,576 bytes |
コンパイル時間 | 2,558 ms |
コンパイル使用メモリ | 193,656 KB |
実行使用メモリ | 38,348 KB |
最終ジャッジ日時 | 2024-12-29 09:29:31 |
合計ジャッジ時間 | 16,554 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 45 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() typedef long long ll; typedef unsigned long long ull; class SCC { int V; std::vector<std::vector<int>> G, rG; std::vector<int> vs; std::vector<bool> used; std::vector<int> cmp; void dfs(int v){ used[v] = true; for(int i=0; i<G[v].size(); i++) if(!used[G[v][i]]) dfs(G[v][i]); vs.push_back(v); } void rdfs(int v, int k){ used[v] = true; cmp[v] = k; for(int i=0; i<rG[v].size(); i++) if(!used[rG[v][i]]) rdfs(rG[v][i], k); } public: SCC(int V):V(V),G(V),rG(V),used(V),cmp(V){} int size(){ return V; } std::vector<int> get_edge(int from){ return G[from]; } void add_edge(int from, int to){ G[from].push_back(to); rG[to].push_back(from); } // 強連結成分(2点が相互に行き来できるグループ)数 int scc(){ for(int i=0; i<used.size(); i++) used[i] = 0; vs.clear(); for(int v=0; v<V; v++) if(!used[v]) dfs(v); for(int i=0; i<used.size(); i++) used[i] = 0; int k = 0; for(int i=vs.size()-1; i>=0; i--) if(!used[vs[i]]) rdfs(vs[i], k++); return k; } // サイクルがあるか bool iscyclic(){ return scc() != V; } // 強連結成分番号 std::vector<int> get_cmp(){ return cmp; } // 同じ辺を2度通らない、その頂点までの最長経路 std::vector<int> depth(){ scc(); std::vector<pair<int,int>> order; std::vector<int> dp(V, 1); for(int i=0; i<V; i++){ order.push_back(make_pair(cmp[i],i)); } std::sort(order.begin(), order.end()); for(int i=0; i<V; i++){ int node = order[i].second; for(int j=0; j<G[node].size(); j++){ dp[G[node][j]] = max(dp[G[node][j]], dp[node]+1); } } return dp; } }; static const ll MOD = 1000000007; vector<vector<ll>> G[100005]; vector<ll> rG[100005]; bool cango1[100005]; bool cango2[100005]; ll dp1[100005]; ll dp2[100005]; int main(){ int N, M; cin >> N >> M; SCC scc(N+1); rep(i,M){ ll a, b, c, d; cin >> a >> b >> c >> d; scc.add_edge(a,b); G[a].push_back({b, c % MOD, d % MOD}); rG[b].push_back(a); } queue<int> que; que.push(N); while(!que.empty()){ int pos = que.front(); que.pop(); if(cango1[pos]) continue; cango1[pos] = true; rep(i,rG[pos].size()){ que.push(rG[pos][i]); } } que.push(0); while(!que.empty()){ int pos = que.front(); que.pop(); if(cango2[pos]) continue; cango2[pos] = true; rep(i,G[pos].size()){ que.push(G[pos][i][0]); } } scc.scc(); vector<int> cmp = scc.get_cmp(); vector<pair<int,int>> order; for(int i=0; i<cmp.size(); i++){ order.push_back(make_pair(cmp[i],i)); } sort(order.begin(), order.end()); vector<bool> visited(N+1,false); visited[0] = true; dp2[0] = 1; rep(ii,order.size()){ int pos = order[ii].second; if(!cango1[pos] || !cango2[pos]) continue; visited[pos] = true; rep(i,G[pos].size()){ ll to = G[pos][i][0]; ll l = G[pos][i][1]; ll a = G[pos][i][2]; if(visited[to]){ cout << "INF" << endl; return 0; } dp1[to] += ((dp1[pos] * a)%MOD + (((a * dp2[pos]) % MOD) * l) % MOD) % MOD; dp1[to] %= MOD; dp2[to] += (dp2[pos] * a) % MOD; dp2[to] %= MOD; } } cout << dp1[N] << endl; return 0; }