dsrvuu Ioencgc_Aeionb
所谓的永别不见得只有死别一种
关注数: 25 粉丝数: 152 发帖数: 6,885 关注贴吧数: 178
aoj2170最优算法 #include<iostream> #include<stack> #include<string.h> #include<cmath> #include<iomanip> #include<algorithm> #include<climits> #include<cstdio> #include<vector> #include<sstream> #include<ctype.h> #include<set> #include<map> #include<ctime> #include<stdlib.h> #include<queue> #include<bitset> #define usi unsigned int #define ull unsigned long long using namespace std; template<typename TTT>inline void mr(TTT& theNumberToRead) { theNumberToRead = 0; bool prn = false; char c = getchar(); while (!isdigit(c)) { if (c == '-')prn = true; c = getchar(); } while (isdigit(c)) theNumberToRead = 10 * theNumberToRead + (c ^ 48), c = getchar(); if (prn) theNumberToRead = -theNumberToRead; } template<typename TTT>inline TTT mrr() { TTT theNumberToRead = 0; bool prn = false; char c = getchar(); while (!isdigit(c)) { if (c == '-')prn = true; c = getchar(); } while (isdigit(c)) theNumberToRead = 10 * theNumberToRead + (c ^ 48), c = getchar(); return prn ? -theNumberToRead : theNumberToRead; } template<typename T>void my_write(T x) { if (x) my_write(x / 10), putchar(x % 10 ^ 48); } template<typename T>void mw(T x, char mid) { if (x) { if (x < 0) putchar('-'), x = -x; my_write(x); } else putchar(48); if (mid) putchar(mid); } // ******************************************华丽的分割线****************************************** // ******************************************华丽的分割线****************************************** // ******************************************华丽的分割线****************************************** // ******************************************华丽的分割线****************************************** // ******************************************华丽的分割线****************************************** int f[100001], m[100001], Q[100001]; int fr(int x, int ti) { register int r = x, t; while (m[r] > ti) r = f[r]; while (x != r) t = x, x = f[x], f[t] = r; return r; } int main() { register int n, q; register long long ans; register char o; f[1] = 1; while (mr(n), mr(q), n) { ans = 0; for (int i = 2; i <= n; ++i) mr(f[i]), m[i] = INT_MAX; for (int i = 1, v; i <= q; ++i) { while (!isalpha(o = getchar())); mr(v); if (o == 'M') m[v] = min(i, m[v]), Q[i] = 0; else Q[i] = v; } for (int i = q; i; --i) if (Q[i]) ans += fr(Q[i], i); mw(ans, '\n'); } return 0; }
首页 1 2 下一页