Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct Con{
- int to;
- string s;
- };
- int n, m;
- vector<vector<Con>> con;
- vector<int> color;
- bool check = false;
- int ans = 0;
- void dfs(int x, int v){
- color[x] = v;
- /*
- Check the nodes that are connected to the current node.
- If the connected node has been checked before, the value must be
- equal if the relation of these nodes are same and the value must
- be different if the relation of these nodes are supposed to be
- different, otherwise the pastures according to the cows are not
- possible and must return.
- If not visited before, send the new node to do dfs for that new
- node and make the value 1 or 2 according to whether the relation
- between these nodes are same or different and whether the current
- node has a value of 1 or 2.
- */
- for(Con c: con[x]){
- if(color[c.to] > 0){
- // cout << "x: " << x << ", c.to: " << c.to << ", color[c.to]: " << color[c.to] << '\n';
- if(c.s == "S" && color[c.to]!=v){
- check = true;
- return;
- }else if(c.s == "D" && color[c.to]==v){
- check = true;
- return;
- }
- }else{
- if(c.s == "S"){
- dfs(c.to, v);
- }else if(c.s == "D"){
- if(v == 1){
- dfs(c.to, 2);
- }else{
- dfs(c.to, 1);
- }
- }
- }
- }
- }
- int main(){
- freopen("revegetate.in", "r", stdin);
- freopen("revegetate.out", "w", stdout);
- cin >> n >> m;
- con.resize(n+1);
- color.resize(n+1, 0);
- for(int i=0; i<m; i++){
- string s;
- int a, b;
- cin >> s >> a >> b;
- con[a].push_back({b, s});
- con[b].push_back({a, s});
- }
- /*
- Use for loop to find the groups by getting values that hasn't been
- used before and sending to dfs function to get the group it is
- connected to
- */
- for(int i=1; i<=n; i++){
- if(color[i] == 0){
- dfs(i, 1);
- if(check){
- // cout << "HI" << '\n';
- cout << 0 << '\n';
- return 0;
- }else{
- // ans*=2;
- ans++;
- }
- }
- }
- // int len = (int)(log2(ans));
- // cout << bitset<64>(ans).to_string().substr(64-ans-1) << '\n';
- // string bi = "";
- // while(ans>0){
- // int bit = ans%2;
- // bi.push_back('0'+bit);
- // ans/=2;
- // }
- // reverse(bi.begin(), bi.end());
- // cout << bi << '\n';
- /*
- The answer of this problem, if not 0, can only be a value that is 2
- to the power of some number, for example (2, 4, 8, 16, 32, 64, etc.).
- Since these values are all 10, 100, 1000, etc. with the amount of 0s
- being the amount that 2 is being powered by, we could just output 1
- and then 0 for the amount of times that the ans must be multiplied
- by itself, becomes the answer. The functions or methods for turning
- a number into binary value, as tried above would not work for this
- problem.
- */
- cout << "1";
- for(int i=0; i<ans; i++){
- cout << "0";
- }
- cout << '\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement