216B - Forming Teams - CodeForces Solution


dfs and similar implementation *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll  long long
#define _for(i, n)      for(ll i = 0; i < n; i++)
#define _forv(i, a, n)      for(ll i = a; i < n; i++)
#define _forvRev(i, a, n)      for(ll i = a; i >= n; i--)
#define _for1(i, n)      for(ll i = 1; i <= n; i++)
#define _fore(k,v,mp)    for (const auto& [k, v] : mp)
#define vbe(vec)    vec.begin(), vec.end()
#define uniqueVec(vec) vec.resize(distance(vec.begin(), unique(vbe(vec))));
#define mp(f,s)     make_pair(f, s)
#define yes     cout<<"Yes"<<endl
#define no      cout<<"No"<<endl
using namespace std;
void decToBinary(ll n,string &s)
{
    int binaryNum[100];
    int i = 0;
    if(n==0)s+="0";
    while (n > 0)
    {
        binaryNum[i] = n % 2;

        n = n / 2;

        i++;

    }
    for (int j = i - 1; j >= 0; j--)

        s+=to_string(binaryNum[j]);
}
inline void f()
{
    ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
}
ll factorial(ll num)
{
    if (num == 0)
        return 1;
    else
        return(num * factorial(num - 1));
}
ll gcd (ll a,ll b)
{
    if(a==0)return b;
    return gcd(b%a,a);
}
ll lcm (ll a,ll b)
{
    return (a*b)/ gcd(a,b);
}
ll binarySearch(ll arr[], ll l, ll r, ll x)
{
    if (r >= l) {
        ll mid = l + (r - l) / 2;

        if (arr[mid] == x)
            return mid;

        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);

        return binarySearch(arr, mid + 1, r, x);
    }

    return -1;
}

vector<ll> G[101];
ll benched=0;
bool fl=true;
map<ll,ll>mp;
ll visited[1001]={0};
void DFS(ll x){
visited[x]=1;
//cout<<x<<"-";
benched++;
_for(i,G[x].size()){
if(!visited[G[x][i]]&&fl)DFS(G[x][i]);
if(G[x].size()==1){fl=false;break;}

}

}
const ll N=1000000;
const double PI =3.14159265;
const ll MOD=1e9+7;

map <ll,ll> mp1;
map<ll,bool> BCD;
map <char,string> mp2;
map <ll,ll> mp3;
vector <ll>v;
vector <ll>vec2;
vector <ll>vec1[101];
queue<char> q;
set<ll> s;
//pair<ll,ll>hourcnt,hourint;
int main()
{
    //freopen(".in", "r", stdin);
    f();
     ll n,m,bench=0;//bool fl= false;
      cin>>n>>m;
     _for(i,m){
         ll a,b;
         cin>>a>>b;
         G[a].push_back(b);
         G[b].push_back(a);
     }
     _for1(i,n){
     DFS(i);
     if(benched%2!=0&&benched>1&&fl){bench++;}
     //cout<<endl;
     //cout<<"("<<benched<<")"<<endl;
     benched=0;
     fl=true;
     }
    if((n-bench)%2==0)cout<<bench<<endl;
    else cout<<bench+1<<endl;
    return 0;
}
/*

*/


Comments

Submit
0 Comments
More Questions

1654C - Alice and the Cake
369A - Valera and Plates
1626A - Equidistant Letters
977D - Divide by three multiply by two
1654B - Prefix Removals
1654A - Maximum Cake Tastiness
1649A - Game
139A - Petr and Book
1612A - Distance
520A - Pangram
124A - The number of positions
1041A - Heist
901A - Hashing Trees
1283A - Minutes Before the New Year
1654D - Potion Brewing Class
1107B - Digital root
25A - IQ test
785A - Anton and Polyhedrons
1542B - Plus and Multiply
306A - Candies
1651C - Fault-tolerant Network
870A - Search for Pretty Integers
1174A - Ehab Fails to Be Thanos
1169A - Circle Metro
780C - Andryusha and Colored Balloons
1153A - Serval and Bus
1487C - Minimum Ties
1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory