1777E - Edge Reverse - CodeForces Solution


binary search dfs and similar graphs

Please click on ads to support us..

C++ Code:

// 2023-01-22
#include <bits/stdc++.h>
#define fastio                    \
	ios_base::sync_with_stdio(0); \
	cin.tie(0);
#define vi vector<int>
#define vl vector<long long>
#define vc vector<char>
#define vs vector<string>
#define pi pair<int, int>
#define pl pair<ll, ll>
#define vp vector<pi>
#define vpl vector<pl>
#define ll long long
#define MAX 2147000000
#define MOD 1000000007
using namespace std;

int main(){
	fastio;
    int t;
    cin >> t;
    while(t--){
        int n, m;
        cin >> n >> m;
        vector<vp> g(n + 1);
        for(int i{0}; i < m; ++i){
            int a, b, c;
            cin >> a >> b >> c;
            g[a].push_back({b, c});
        }
        int l{0}, r{(int)1e9};
        int ans{-1};
        while(l <= r){
            int mid{(l + r) / 2};
            vector<vi> g2(n + 1);
            for(int i{1}; i <= n; ++i){
                for(auto& j : g[i]){
                    g2[i].push_back(j.first);
                    if(j.second <= mid){                        
                        g2[j.first].push_back(i);
                    }
                }
            }  
            vi visited(n + 1);
            int root; 
            function<void(int)> dfs = [&](int cur){
                for(auto& i : g2[cur]){
                    if(visited[i] == 0){
                        visited[i] = 1;
                        dfs(i);
                    }
                }
                root = cur;
            };
            for(int i{1}; i <= n; ++i){
                if(!visited[i]){
                    visited[i] = 1;
                    dfs(i);
                }
            }
            vi ch(n + 1);
            ch[root] = 1;
            queue<int> Q;
            int cnt{0};
            Q.push(root);
            while(!Q.empty()){
                int x{Q.front()};
                Q.pop();
                cnt++;
                for(auto& i : g2[x]){
                    if(ch[i] == 0){
                        ch[i] = 1;
                        Q.push(i);
                    }
                }
            }
            if(cnt == n){
                ans = mid;
                r = mid - 1;
            }
            else l = mid + 1;
        }
        cout << ans << "\n";
    }
}


Comments

Submit
0 Comments
More Questions

1728D - Letter Picking
792B - Counting-out Rhyme
1195A - Drinks Choosing
5D - Follow Traffic Rules
1272A - Three Friends
1632D - New Year Concert
1400D - Zigzags
716C - Plus and Square Root
412A - Poster
844B - Rectangles
1591A - Life of a Flower
1398C - Good Subarrays
629A - Far Relative’s Birthday Cake
1166A - Silent Classroom
1000B - Light It Up
218B - Airport
1463B - Find The Array
1538C - Number of Pairs
621B - Wet Shark and Bishops
476B - Dreamoon and WiFi
152C - Pocket Book
1681D - Required Length
1725D - Deducing Sortability
1501A - Alexey and Train
721B - Passwords
1263D - Secret Passwords
1371B - Magical Calendar
1726E - Almost Perfect
1360C - Similar Pairs
900A - Find Extra One