vendredi 24 janvier 2020

Execution killed (could be triggered by violating memory limits)"" problem

What is getting me this problem. Limit is n < 1000 m < 499500

I'm trying to solve a problem. (There is n people

Some of them know each other

Can man only know woman and woman only know man)

.................................................................. ................................................................. ..................

#include <iostream>
#include <vector>
#include <algorithm>
vector<int> gender;
vector<vector<int> > friends;

bool check(int index) {
    int now = 1 - gender[index];

    for (int i = 0; i < friends[index].size(); i++) {
        if (gender[friends[index][i]] != -1) {
            if (gender[friends[index][i]] != now)
                return false;
            continue;
        } else {
            gender[friends[index][i]] = now;
        }

        if (!check(friends[index][i])) {
            return false;
        }
    }

    return true;
}   

int main(){
    int n, m;
    cin >> n >> m;
    gender.resize(n, -1);
    friends.resize(n);

    int a, b, firstguy;

    for (int i = 0; i < m; i++) {
        cin >> a >> b;
        a--;
        b--;
        friends[a].push_back(b);
        friends[b].push_back(a);
    }
    int i;
    for (i = 0; i < n; i++) {
        if (friends[i].size() != 0) {
            gender[i] = 1;
            int count = 1;
            while (count != 0) {
                count = 0;
                if (!check(i)) {
                    cout << "NO\n";
                    return 0;
                }
                for (int j = 0; j < n; j++) {
                    if (gender[j] == -1) {
                        gender [j] = 1;
                        i = j;
                        count = 1;
                        break;
                    }
                }
            }
            break;
        }
    }
    cout << "YES\n";
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire