Cod sursa(job #1710079)

Utilizator UNIBUC_Echipa_LachetaBicsi Nitu Velea UNIBUC_Echipa_Lacheta Data 28 mai 2016 14:58:08
Problema Robo Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 4.2 kb
#include <cassert>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <bitset>
#include <ctime>
#include <set>
#include <cmath>
#include <iomanip>
#include <map>
#include <stack>
#include <vector>
#include <bitset>

#include <fstream>

using namespace std;

#define FOR(i, a, n) for (int i = a; i <= n; ++i)
#define ROF(i, n, a) for (int i = n; i >= a; i--)
#define FIT(i, v) for (auto &i : v)
#define pb push_back
#define mp make_pair
#define mt make_touple
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
typedef long long ll;
typedef pair<int,int> pii;
const int mod = 2000003;
ll powmod(ll a, ll b) {ll res=1; a %= mod; assert(b >= 0); for(; b; b >>= 1) {if (b & 1) res = res * a % mod; a = a * a % mod;} return res;}

const int N = 6000;

ifstream fin("robo.in");
ofstream fout("robo.out");
const int DIM = 10000000;
char s[DIM];
int p, len;
bool dest[N];
vector<int> rev[N][10];
bool same[N][N];
bool enaspa;


bool isDigit(char c) {
    return ('0' <= c && c <= '9');
}

int getInt() {
    while (!isDigit(s[p])) {
        ++p;
    }
    int x = 0;
    while (isDigit(s[p])) {
        x = x * 10 + (s[p++] - '0');

    }
    return x;
}

int getIntn() {
    if (s[p] == '\n') {
        return -1;
    }
    while (!isDigit(s[p])) {
        if (s[p] == '\n') {
            return -1;
        }
        ++p;
        if (s[p] == '\n') {
            return -1;
        }
    }
    int x = 0;
    while ( isDigit(s[p])) {
        x = x * 10 + (s[p++] - '0');
        
    }
    return x;
}

char getChar() {
    while (s[p] == ' ')
        ++p;
    char c = s[p];
    ++p;
    return c;
}

int get(map<char,int> &M, char c) {
    if (M.find(c) == M.end()) {
        int x = M.size();
        M[c] = x;
        return x;
    } else {
        return M[c];
    }
}

int main() {
    fin.get(s, DIM, EOF);
    int t = getInt();
    while (t--) {
        
        int n = getInt();
        int m = getInt();
        
        for (int i = 0; i < n; ++i) {
            dest[i] = false;
        }
        while (!isDigit(s[p])) {
            ++p;
        }
//        readLine();
        int x = getIntn();
        do {
            dest[x] = true;
            x = getIntn();
        } while(x != -1);
        //
        //        for (int i = 0; i < n; ++i) {
        //            fout << dest[i] << " ";
        //        }
        //        fout << "\n";
        
        
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                rev[i][j].clear();
            }
        }
        
        queue<pii> Q;
        for (int i = 0; i < n; ++i) {
            for (int j = i; j < n; ++j) {
                same[i][j] = (dest[i] == dest[j]);
                if (!same[i][j]) {
                    Q.push({i, j});
                }
            }
        }
        
        map<char, int> M;
        for (int i = 1; i <= n*m; ++i) {
//            readLine();
            int x = getInt();
            char c = getChar();
            int y = getInt();
            int wh = get(M, c);
            //fout << x << c << y << "\n";
            rev[y][wh].push_back(x);
        }
        
        while(!Q.empty()) {
            int x = Q.front().first;
            int y = Q.front().second;
            Q.pop();
            
            for (int c = 0; c < m; ++c) {
                for (auto xx : rev[x][c]) {
                    for (auto yy : rev[y][c]) {
                        if (xx > yy) {
                            xx ^= yy;
                            yy ^= xx;
                            xx ^= yy;
                        }
                        if (same[xx][yy]) {
                            same[xx][yy] = false;
                            Q.push({xx, yy});
                        }
                    }
                }
            }
        }
        
        int ans = 0;
        vector<bool> distinct(n, true);
        for (int i = 0; i < n; ++i) {
            if (distinct[i]) {
                ++ans;
                for (int j = i+1; j < n; ++j) {
                    if (same[i][j]) {
                        distinct[j] = false;
                    }
                }
            }
        }
        
        fout << ans << "\n";
    }
}