1361A - Johnny and Contribution - CodeForces Solution


constructive algorithms graphs greedy sortings *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define ll long long
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define forn1(i, n) for (int i = 1; i <= (n); ++i)
#define pb push_back
#define max(a,b) ((a)>(b)? (a):(b))
#define min(a,b) ((a)<(b)? (a):(b))
#define all(v) v.begin(),v.end()
#define rall(x) (x).rbegin(), (x).rend()
const int mod = 1e9+7 ;

void print_vector(vector<int>v){
    for(int i=0;i<v.size();i++){
        cout<<v[i]<<" ";
    }
    cout<<endl;
}
void print_arr(int arr[], int n){
    for(int i=0;i<n;i++){
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}
void print_vector_pair(vector<pair<int,int>>v){
    for(int i=0;i<v.size();i++){
        cout<<v[i].first<<" "<<v[i].second<<" "<<endl;
    }
    cout<<endl;
}

// int kmp(string s){
//     vector<int>lps(s.size(),0);
//     for(int i=1;i<lps.size();i++){
//         int prev_index = lps[i-1];
//         while(prev_index>0 && s[i]!=s[prev_index]){
//             prev_index = lps[prev_index-1];
//         }
//         lps[i] = prev_index + (s[i]==s[prev_index]?1:0);
//     }
//     return lps[lps.size()-1];
// }



void solve(){
    int n,m;
    cin>>n>>m;
    vector<int>gr[n+1];
    for(int i=0;i<m;i++){
        int u,v;
        cin>>u>>v;
        gr[u].pb(v);
        gr[v].pb(u);
    }
        vector<ll> v[n + 1];
    ll colour_graph[n + 1];
    for (ll i = 1; i <= n; i++) {
        ll colour;
        cin >> colour;
 
        colour_graph[i] = colour;
        v[colour].push_back(i);
    }
 
    ll flag = 0;
    vector<ll> result;
    for (ll i = 1; i <= n; i++) {
        for (auto nodes : v[i]) {
 
            set<ll> s;
            for (auto neigh : gr[nodes]) {
                if (colour_graph[neigh] < colour_graph[nodes]) {
                    s.insert(colour_graph[neigh]);
                }
                if (colour_graph[neigh] == colour_graph[nodes]) {
                    flag = 1;
                    break;
                }
            }
 
            if (s.size() != colour_graph[nodes] - 1) {
                flag = 1;
                break;
            }
 
            result.push_back(nodes);
        }
    }
 
    if (flag == 1) {
        cout << "-1" << "\n";
    } else {
        for (ll i = 0; i < result.size(); i++) {
            cout << result[i] << " ";
        }
    }

}  

signed main(){
    IOS
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int tests;
    tests = 1;
    // cin>>tests;
    while(tests--){
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal
96. Unique Binary Search Trees
75. Sort Colors
74. Search a 2D Matrix
71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree