445B - DZY Loves Chemistry - CodeForces Solution


dfs and similar dsu greedy *1400

Please click on ads to support us..

C++ Code:

//بسم الله الرحمن الرحيم
// i love mancity
#include<bits/stdc++.h>
#include <stack>
#include <vector>
#define endl "\n"
#define ll long long
#define f(a) for(int i=0;i<a;i++)
#define rf(a) for(int i=a-1;i>=0;i--)
#define w  int t;cin>>t;while(t--)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define prev2pn(x) pow(2,floor(log2(x)))
#define MR_Robot iostream::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MA INTMAX
#define Mn INTMIN
using namespace std;
const ll MOD=1e9+7;
const int N=1000;
bool test[N];
//int e,n,Minx=INT_MAX,Maxx=INT_MIN,Miny=INT_MAX,Maxy=INT_MIN;
const int mxx=1000005;
//ll a[mx];
//vector<vector<ll>>v;
ll power(ll x, ll y,ll mod)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = power(x, y / 2,mod)%mod;
    if (y % 2 == 0)
        return (temp * temp)%mod;
    else
        return ((x * temp )%mod* temp)%mod;
}
set<ll>sv;
void sieve(ll x){

    bool primes[x+5];
    memset(primes,false,sizeof primes);
    for (ll i = 2; i*i <=x ; ++i) {
        if(!primes[i]){
            for (ll j = i*i; j <=x ; j+=i) {
                primes[j]= true;
            }
        }
    }
    for (ll i = 2; i <=x ; ++i) {
        if(!primes[i]){
            sv.insert(i*i);
        }
    }
}

set<long long> lucky_numbers;

void generate_lucky_number() {
    for (int i = 0; i <= 10; i++) {
        for (int j = 0; j < (1 << i); ++j) {
            string s;
            int cnt1 = 0, cnt2 = 0;
            for (int k = 0; k < i; ++k) {
                if ((1 << k) & j) {
                    s += "7";
                    cnt1++;
                } else {
                    s += "4";
                    cnt2++;
                }
            }
            if (!s.empty())lucky_numbers.insert(stoll(s));
        }
    }
}

ll solve2(ll a) {
    ll cnt=0;
    for (ll i = 2; i*i <= a ; ++i) {
        if(a%i==0){
            if(i*i==a){
                cnt++;
            }else{
                cnt+=2;
            }
        }
    }
    return cnt;
}
vector<int>adj[N];
bool vis[N];
bool ff[N];
vector<int>top;
int color[N];
ll n,m,cnt=1,mx=INT_MIN;
bool f=false;
bool invalid(int x,int y){
    if(x<0||x>=n||y<0||y>=m)return false;
    else return true;
}
void dfs(int node){
    vis[node]= true;
    for (auto u:adj[node]) {
        if (!vis[u]) {
            cnt*=2;
            dfs(u);
        }
    }
}
void solve(){
    cin>>n>>m;
    for (int i = 0; i < m; ++i) {
        int x, y;
        cin >> x >> y;
        x--; y--;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    for (int i = 0; i < n; ++i) {
        if (!vis[i]) {
            dfs(i);
          
        }
    }
    cout<<cnt<<endl;

}
int main() {
    MR_Robot
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int z=1;
//    w {

    solve();
//    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

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
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy