427C - Checkposts - CodeForces Solution


dfs and similar graphs two pointers *1700

Please click on ads to support us..

Python Code:

n, m = int(input()) + 1, 1000000007
q, p = [[] for i in range(n)], [[] for i in range(n)]
w = [0] + list(map(int, input().split()))
for i in range(int(input())):
    u, v = map(int, input().split())
    p[u].append(v)
    q[v].append(u)
r = set(i for i in range(1, n) if not p[i] or not q[i])
s, t = 0, 1
while r:
    i = r.pop()
    s += w[i]
    for j in p[i]:
        q[j].remove(i)
        if not q[j]: r.add(j)
    for j in q[i]:
        p[j].remove(i)
        if not p[j]: r.add(j)
r = set(i for i in range(1, n) if p[i] and q[i])
while r:
    i = r.pop()
    h = p[i]
    d, k = w[i], 1
    while h:
        i = h.pop()
        if not i in r: continue
        r.remove(i)
        h += p[i]
        if w[i] == d: k += 1
        elif w[i] < d: d, k = w[i], 1
    s += d
    t = (t * k) % m
print(s,t)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long int
const int mod=1e9+7;
map<int,int> mp;
void dfs(int node,vector<int> &vis,vector<int> g[],stack<int> &st){
    vis[node]=1;
    for(auto it:g[node]){
        if(vis[it])continue;
        dfs(it,vis,g,st);
    }
    st.push(node);
}


void Dfs(int val,vector<int> &vis,vector<int> g[],int &mini,vector<int> &c){
    vis[val]=1;
    mp[c[val]]++;
    mini=min(mini,c[val]);
    for(auto it:g[val]){
        if(vis[it])continue;
        Dfs(it,vis,g,mini,c);
    }
}

void Solve(){
   int n;
   cin>>n;

   vector<int> cost(n+1);

   for(int i=1;i<=n;i++)cin>>cost[i];

   int m;
   cin>>m;

   vector<int> g[n+1],rev[n+1];

   for(int i=1;i<=m;i++){
    int u,v;
    cin>>u>>v;
    g[u].push_back(v);
    rev[v].push_back(u);
   }

   stack<int> st;
   vector<int> vis1(n+1,0);
  for(int i=1;i<=n;i++){
    if(!vis1[i]){
        dfs(i,vis1,g,st);
    }
}
   vector<int> vis(n+1,0);
   int ans=0,res=1;
   while(st.size()>0){
    int val=st.top();
    // cout<<val<<endl;
    st.pop();
    if(vis[val])continue;
    mp.clear();
    int mini=9e9;
    Dfs(val,vis,rev,mini,cost);
    ans+=mini;
    res*=mp[mini];
    res=res%mod;
   }
   cout<<ans<<" "<<res<<endl;
}



int32_t main(){
    int T=1;
    // cin>>T;
    while(T--){
        Solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One
131A - cAPS lOCK
1635A - Min Or Sum
474A - Keyboard
1343A - Candies
1343C - Alternating Subsequence
1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm