教育超市买了一套,1、2忘本地存了。prime和静态链表,较为简单。
3 电信诈骗
一开始没用DFS,每个团体存储的只是查询点的邻近点,有一个6分的测试点过不去。
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <cstring>
#include <cctype>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
const int MAXV = 1010;
int k, n, m;
int G[MAXV][MAXV] = {0}, vis[MAXV] = {0};
set<set<int>> ans;
set<int> s, t;
void detect(int u) {
int c1 = 0, c2 = 0;
for(int v = 1; v <= n; v++) {
if(G[u][v] && G[u][v] <= 5) {
c1++;
if(G[v][u]) c2++;
}
}
if(c1 > k && 1.0 * c2 / c1 <= 0.2) s.insert(u);
}
void printSet(set<int> &s) {
int f = 0;
for(auto it : s) {
if(f) printf(" ");
printf("%d", it);
vis[it] = 1;
f = 1;
}
printf("\n");
}
void printSS(set<set<int>> &s) {
for(auto it : s) printSet(it);
}
void DFS(int u) {
vis[u] = 1;
t.insert(u);
for(auto v : s)
if(!vis[v] && G[u][v] && G[v][u])
DFS(v);
}
void detectGang() {
for(auto u : s) {
if(!vis[u]) {
t.clear();
DFS(u);
if(t.size()) ans.insert(t);
}
}
}
int main() {
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
#endif
scanf("%d%d%d", &k, &n, &m);
while(m--) {
int a, b, c; scanf("%d%d%d", &a, &b, &c);
G[a][b] += c;
}
for(int i = 1; i <= n; i++)
detect(i);
if(s.size()) {
// printSet(s);
detectGang();
printSS(ans);
} else printf("None\n");
return 0;
}
4
结构体里另外存储father、layer,后中序构树时候就存进去,full和root也构树时候就存储。
静态数组便于直接查询。
抠字符串有点烦。后来发现substr只要str开头是数字,后面的一串自动忽略;开头有几个空格也会自动忽略。
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <cstring>
#include <cctype>
#include <map>
#include <unordered_map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
int n, k;
vector<int> in, post;
map<int,int> ht;
int full = 1, root;
struct node {
int data, layer, father;
int lchild, rchild;
}no[1010];
void create(int &root, int inL, int inR, int postL, int postR, int layer, int father) {
int inRoot = ht[post[postR]];
root = in[inRoot];
no[root].layer = layer;
no[root].father = father;
int cnt = 0;
if(inL < inRoot) {
create(no[root].lchild , inL, inRoot - 1, postL, postR - 1 - (inR - inRoot), layer + 1, root);
cnt++;
}
if(inR > inRoot) {
create(no[root].rchild, inRoot + 1, inR, postR - (inR - inRoot), postR - 1, layer + 1, root);
cnt++;
}
if(cnt == 1) full = 0;
}
int main() {
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
#endif
scanf("%d", &n); in.resize(n+1); post.resize(n+1);
for(int i = 1; i <= n; i++) scanf("%d", &post[i]);
for(int i = 1; i <= n; i++) {
scanf("%d", &in[i]);
ht[in[i]] = i;
}
create(root, 1, n, 1, n, 1, -1);
scanf("%d\n", &k);
while(k--) {
string s; getline(cin, s);
if(s.find("root") != -1) {
if(root == stoi(s)) printf("Yes\n");
else printf("No\n");
}
else if(s.find("siblings") != -1) {
int pos = s.find("and");
int a = stoi(s.substr(0, pos));
int b = stoi(s.substr(pos + 3));
if(no[a].father == no[b].father) printf("Yes\n");
else printf("No\n");
}
else if(s.find("same level") != -1) {
int pos = s.find("and");
int a = stoi(s.substr(0, pos));
int b = stoi(s.substr(pos + 3));
if(no[a].layer == no[b].layer) printf("Yes\n");
else printf("No\n");
}
else if(s.find("full tree") != -1) {
if(full) printf("Yes\n");
else printf("No\n");
}
else if(s.find("parent") != -1) {
int pos = s.find(" is ");
int a = stoi(s.substr(0, pos));
pos = s.find("of ");
int b = stoi(s.substr(pos + 3));
if(no[b].father == a) printf("Yes\n");
else printf("No\n");
}
else if(s.find("left") != -1) {
int pos = s.find(" is ");
int a = stoi(s.substr(0, pos));
pos = s.find("of ");
int b = stoi(s.substr(pos + 3));
if(no[b].lchild == a) printf("Yes\n");
else printf("No\n");
}
else if(s.find("right") != -1) {
int pos = s.find(" is ");
int a = stoi(s.substr(0, pos));
pos = s.find("of ");
int b = stoi(s.substr(pos + 3));
if(no[b].rchild == a) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}