1926G - Vlad and Trouble at MIT - CodeForces Solution


dfs and similar dp graphs implementation trees

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std; 
#define ll long long 
#define all(n) n.begin(),n.end()
void solve(){
 int n; cin>>n; 
    char S; cin>>S; 
    vector<char>  CH = {'C','D','H','S'}; 
    map<char,vector<string>>vsp; 
    vector<pair<string,string>>out; 

    for(int i = 0; i < n * 2; i++){
        string str; cin>>str; 
        vsp[str[1]].push_back(str); 
    }

    int cnt = 0; 

    for(int i = 0; i < 4; i++){
        sort(vsp[CH[i]].begin(),vsp[CH[i]].end()); 
    }

    for(int i = 0; i < 4; i++){
        if(CH[i] != S)
            if(vsp[CH[i]].size() % 2 != 0) cnt++; 
    }

    if(vsp[S].size() % 2 != cnt % 2 or cnt > vsp[S].size()){
        cout<<"IMPOSSIBLE\n"; 
        return ; 
    }

    for(int i = 0; i < 4; i++){
        if(CH[i] != S && vsp[CH[i]].size() % 2 != 0){
            cout<<vsp[CH[i]][vsp[CH[i]].size() - 1]<<' ';
            cout<<vsp[S][vsp[S].size() - 1]<<'\n';
            vsp[CH[i]].pop_back();
            vsp[S].pop_back();  
        }
    }

    for(int i = 0; i < 4; i++){
        if(CH[i] != S){
            for(int j = 0; j < vsp[CH[i]].size(); j+=2){
                cout<<vsp[CH[i]][j]<<' '; 
                cout<<vsp[CH[i]][j+1]<<'\n'; 
            }
        }
    }

    for(int i = 0; i < vsp[S].size(); i+=2){
        cout<<vsp[S][i]<<' '; 
        cout<<vsp[S][i+1]<<'\n'; 
    }

}
int N = 2e5+5; 
vector<ll>ans(N); 
ll dig(int i){
  int sum = 0; 
  while(i){
    sum+=i%10; 
    i/=10; 
  }
 return sum; 
}
bool check(vector<vector<char>>& v, int idx) {
    for (int i = 1; i < v.size() - 1; ++i) {
        for (int j = 1; j < v.size() - 1; ++j) {
            if (((i + j) & 1) == idx) {
                if (v[i][j] == 'B' && v[i - 1][j - 1] == 'B' &&
                 v[i - 1][j + 1] == 'B' && v[i + 1][j - 1] == 'B'
                  && v[i + 1][j + 1] == 'B') {
                    return 0;
                }
            }
        }
    }
    return 1;
}
const int MAX = 1e5 + 10;
string problem;
vector<int> children[MAX];
vector<int> dp_first(MAX),dp_second(MAX);

void dfs(int root = 0){
    
    dp_first[root] = 0;
    dp_second[root] = 0;

    for(auto child : children[root]){
            dfs(child);
            dp_first[root] += min(dp_second[child]+1, dp_first[child]);
            dp_second[root] += min(dp_first[child]+1, dp_second[child]);
        
    }

    if(problem[root] == 'S'){
        dp_first[root] = 1e6;
    }else if(problem[root] == 'P'){
        dp_second[root] = 1e6;
    }
}
void solve2(){
   //  int cnt = 0; 
   // vector<vector<char>> a(7, vector<char>(7));
   //        vector<pair<int, int>> o, e;
   //      for (int i = 0; i < 7; ++i) {
   //          for (int j = 0; j < 7; ++j) {
   //              cin >> a[i][j];
   //          }
   //      }

   //      for (int i = 1; i < 6; ++i) {
   //          for (int j = 1; j < 6; ++j) {
   //              if ((i + j) & 1 && a[i][j] == 'B') {
   //                  o.push_back({i, j});
   //              } else if ((i + j) % 2 == 0 && a[i][j] == 'B') {
   //                  e.push_back({i, j});
   //              }
   //          }
   //      }


   //      int mx = 1e9+5; 
   //      for (int i = 0; i < (1 << int(o.size())); ++i) {
   //           cnt = 0;
   //          for (int j = 0; j < o.size(); ++j) {
   //              if ((1 << j) & i) {
   //                  a[o[j].first][o[j].second] = 'W';
   //                  cnt++;
   //              }
   //          }
   //          mx = (check(a,1) ? min(mx,cnt) : mx); 
          
   //          for (int j = 0; j < o.size(); ++j) {
   //              if ((1 << j) & i) {
   //                  a[o[j].first][o[j].second] = 'B';
   //              }
   //          }
   //      }

   //       int mx_ = 1e9+5; 

   //      for (int i = 0; i < (1 << int(e.size())); ++i) {
   //          cnt = 0;
   //          for (int j = 0; j < e.size(); ++j) {
   //              if ((1 << j) & i) {
   //                  a[e[j].first][e[j].second] = 'W';
   //                  cnt++;
   //              }
   //          }
   //          mx_ = (check(a,0) ? min(mx_,cnt) : mx_); 
   //          for (int j = 0; j < e.size(); ++j) {
   //              if ((1 << j) & i) {
   //                  a[e[j].first][e[j].second] = 'B';
   //              }
   //          }
   //      }

   //      cout << mx + mx_ << '\n';
     int num;
    cin>>num;
    int temp;
    for(int i = 0;i<num;i++){
        children[i].clear();
        if(i != 0){
            cin>>temp;
            children[temp-1].push_back(i);
        }
    }
    cin>>problem;

    dfs();

    cout<<min(dp_first[0],dp_second[0])<<"\n";
        
}
int main() {
   int tt; cin>>tt; 
     while(tt--){
      solve2(); 
     }
 
   // 1000/
    return 0;
}


Comments

Submit
0 Comments
More Questions

MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image