Skip to content

Commit

Permalink
add final exam AC code
Browse files Browse the repository at this point in the history
  • Loading branch information
frankkn committed Jan 4, 2024
1 parent cb97f12 commit 71836a6
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
62 changes: 62 additions & 0 deletions exercise/final/final.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <iostream>
#include <vector>
#include <map>
#include <cstring>
using namespace std;

int G[103][103];
vector<int> visited;
map<int, int> mp;
int n;

bool flag = false;

void dfs(int now, int end){
visited[now] = 1;
mp[now]++;
if(now == end){
flag = true;
return;
}
for(int i = 0; i < n; ++i){
if(G[now][i] && !visited[i]){
dfs(i, end);
}
}
visited[now] = 0;
if(!flag) mp[now]--;
}

int main(){
int t; cin >> t;
while(t--){
memset(G, 0, sizeof(G));
cin >> n;
visited.assign(n+1, 0);
for(int i = 0; i < n-1; ++i){
int x, y; cin >> x >> y;
G[x][y] = 1;
G[y][x] = 1;
}
int q; cin >> q;
while(q--){
visited.assign(n+1, 0);
mp.clear();
int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
dfs(x1, x2);
visited.assign(n+1, 0);
dfs(y1, y2);
bool print = false;
for(auto [x, y]: mp){
if(y == 2){
print = true;
cout << "A\n"
break;
}
}
if(!print) cout << "B\n";
}
}

return 0;
}
68 changes: 68 additions & 0 deletions exercise/final/final2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include <iostream>
#include <vector>
#include <map>
#include <cstring>
using namespace std;

int G[103][103];
vector<int> visited;
int n;
int prev[103];

void dfs(int now){
visited[now] = 1;
for(int i = 0; i < n; ++i)
if(G[now][i] && !visited[i])
dfs(i), prev[i] = now;
}

int main(){
int t; cin >> t;
while(t--){
memset(G, 0, sizeof(G));
cin >> n;
visited.assign(n+1, 0);
for(int i = 0; i < n-1; ++i){
int x, y; cin >> x >> y;
G[x][y] = 1;
G[y][x] = 1;
}
int q; cin >> q;
while(q--){
int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;
vector<int> p1, p2;
memset(prev, 0, sizeof(prev));
visited.assign(n+1, 0);
dfs(x1);
int start = prev[x1] = -1;
int end = x2;
while(start != end){
p1.push_back(end);
end = prev[end];
}

memset(prev, 0, sizeof(prev));
visited.assign(n+1, 0);
dfs(y1);
start = prev[y1] = -1;
end = y2;
while(start != end){
p2.push_back(end);
end = prev[end];
}

for(auto x:p1){
for(auto y:p2){
if(x==y){
cout << "A\n";
goto end;
}
}
}
cout << "B\n";
end:;
}
}

return 0;
}

0 comments on commit 71836a6

Please sign in to comment.