E
判断是否有死循环
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <iomanip> #include <deque> #include <bitset> #define ll long long #define PII pair<int, int> #define rep(i,a,b) for(int i=a;i<=b;i++) #define dec(i,a,b) for(int i=a;i>=b;i--) #define pb push_back #define mk make_pair using namespace std; int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } }; const long long INF = 0x7f7f7f7f7f7f7f7f; const int inf = 0x3f3f3f3f; const double pi = 3.14159265358979323846; const int mod = 998244353; const int N = 50 + 5; //if(x<0 || x>=r || y<0 || y>=c) inline ll read() { ll x = 0; bool f = true; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? x : -x; } ll gcd(ll m, ll n) { return n == 0 ? m : gcd(n, m % n); } ll lcm(ll m, ll n) { return m * n / gcd(m, n); } int main() { int t; cin >> t; rep(q, 1, t) { int p, s; cin >> p >> s; vector<string> name(p + 1); vector<vector<int>> g(p + 1,vector<int>(p+1,0)); rep(i, 1, p) { cin >> name[i]; } rep(i, 1, p) { int n; cin >> n; rep(j, 1, n) { int t; cin >> t; g[i][t] = 1; } } rep(i, 1, p) { rep(j, 1, p) { rep(k, 1, p) { g[j][k] |= g[j][i] & g[i][k]; } } } vector<bool> cycle(p + 1,0); rep(i, 1, p) { if (g[i][i]) cycle[i] = 1; } vector<bool> infi(p + 1,0); rep(i, 1, p) { if (g[s][i] && cycle[i]) infi[i] = 1; } bool change = 1; while (change) { change = 0; rep(i, 1, p) { if (infi[i]) { rep(j, 1, p) { if (g[i][j] && !infi[j]) { infi[j] = 1; change = 1; } } } } } int cnt = 0; rep(i, 1, p) { if (infi[i]) cnt++; } if (cnt == 0) cout << "Chain Email #" << q << ": " << "Safe chain email!" << endl; else { cout << "Chain Email #" << q << ":" << endl; rep(i, 1, p) { if (infi[i]) cout << name[i] << " "; } cout << endl; } cout << endl; } return 0; }
J
对阶乘计算
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <queue> #include <map> #include <sstream> #include <cstdio> #include <cstring> #include <numeric> #include <cmath> #include <iomanip> #include <deque> #include <bitset> #define ll long long #define PII pair<int, int> #define rep(i,a,b) for(int i=a;i<=b;i++) #define dec(i,a,b) for(int i=a;i>=b;i--) #define pb push_back #define mk make_pair using namespace std; int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } }; const long long INF = 0x7f7f7f7f7f7f7f7f; const int inf = 0x3f3f3f3f; const double pi = 3.14159265358979323846; const int mod = 998244353; const int N = 50 + 5; //if(x<0 || x>=r || y<0 || y>=c) inline ll read() { ll x = 0; bool f = true; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return f ? x : -x; } ll gcd(ll m, ll n) { return n == 0 ? m : gcd(n, m % n); } ll lcm(ll m, ll n) { return m * n / gcd(m, n); } int main() { int t; cin >> t; rep(q, 1, t) { int p, s; cin >> p >> s; vector<string> name(p + 1); vector<vector<int>> g(p + 1,vector<int>(p+1,0)); rep(i, 1, p) { cin >> name[i]; } rep(i, 1, p) { int n; cin >> n; rep(j, 1, n) { int t; cin >> t; g[i][t] = 1; } } rep(i, 1, p) { rep(j, 1, p) { rep(k, 1, p) { g[j][k] |= g[j][i] & g[i][k]; } } } vector<bool> cycle(p + 1,0); rep(i, 1, p) { if (g[i][i]) cycle[i] = 1; } vector<bool> infi(p + 1,0); rep(i, 1, p) { if (g[s][i] && cycle[i]) infi[i] = 1; } bool change = 1; while (change) { change = 0; rep(i, 1, p) { if (infi[i]) { rep(j, 1, p) { if (g[i][j] && !infi[j]) { infi[j] = 1; change = 1; } } } } } int cnt = 0; rep(i, 1, p) { if (infi[i]) cnt++; } if (cnt == 0) cout << "Chain Email #" << q << ": " << "Safe chain email!" << endl; else { cout << "Chain Email #" << q << ":" << endl; rep(i, 1, p) { if (infi[i]) cout << name[i] << " "; } cout << endl; } cout << endl; } return 0; }