1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <cstdlib> #include <algorithm> #include <cstring> #include <cctype> #include <set> #include <map> #define INF 2000000000 using namespace std; typedef long long ll; int (){ int f = 1, x = 0; char c = getchar(); while(c < '0' || c > '9'){if(c == '-') f = -f; c = getchar(); } while(c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar(); return f * x; } typedef set<int> Set; map<Set, int> mp; int n, stack[10005], top, tot; Set st[2005]; int getID(Set *s){ if(!mp.count(*s)){ mp[*s] = tot, tot++; return tot - 1; } return mp[*s]; } void push(){ stack[top++] = 0; } void dup(){ stack[top] = stack[top - 1]; top++; } void _union(){ int xa, xb, id; xa = stack[--top], xb = stack[--top]; Set ss; set_union(st[xa].begin(), st[xa].end(), st[xb].begin(), st[xb].end(), inserter(ss, ss.begin())); id = getID(&ss); st[id] = ss; stack[top++] = id; } void _inter(){ int xa, xb, id; xa = stack[--top], xb = stack[--top]; Set ss; set_intersection(st[xa].begin(), st[xa].end(), st[xb].begin(), st[xb].end(), inserter(ss, ss.begin())); id = getID(&ss); st[id] = ss; stack[top++] = id; } void add(){ int xa, xb, id; xa = stack[--top], xb = stack[--top]; Set ss = st[xb]; ss.insert(xa); id = getID(&ss); st[id] = ss; stack[top++] = id; } void init(){ n = read(); top = tot = 1; mp.clear(); Set s; mp[s] = 0, st[0] = s; } void solve(){ char opr[12]; for(int i = 0; i < n; ++i){ scanf("%s", opr); if(opr[0] == 'P')push(); if(opr[0] == 'D')dup(); if(opr[0] == 'U')_union(); if(opr[0] == 'I')_inter(); if(opr[0] == 'A')add(); printf("%dn", st[stack[top - 1]].size()); } printf("***n"); } int main(){ int T = read(); while(T--){ init(); solve(); } return 0; }
|
近期评论