QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#354844 | #6559. A Tree and Two Edges | kevinshan# | WA | 81ms | 10924kb | C++17 | 5.7kb | 2024-03-16 05:07:50 | 2024-03-16 05:07:51 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(), x.end()
#define pb push_back
#define f first
#define s second
const int MAXN = 50005;
vector<int> adj[MAXN];
int vis[MAXN];
vector<pair<int, int>> dbs;
void dfs(int x, int p) {
vis[x] = 1;
for(int i:adj[x]){
if(i == p) continue;
if(vis[i]) {
dbs.pb({x,i});
} else {
dfs(i,x);
}
}
}
set<int> ig;
vector<set<int>> get_join(int a, int b, int n) {
vector<int> dsta(n,-1);
vector<int> dstb(n,-1);
queue<int> q;
if(ig.find(a) == ig.end()){
q.push(a);
dsta[a] = 0;}
while(q.size()) {
int c = q.front();
q.pop();
for(int i:adj[c]) {
if(ig.find(i) != ig.end()) continue;
if(dsta[i] == -1) {
dsta[i] = dsta[c] + 1;
q.push(i);
}
}
}
if(ig.find(b) == ig.end()){
q.push(b);
dstb[b] = 0;}
while(q.size()) {
int c = q.front();
q.pop();
for(int i:adj[c]) {
if(ig.find(i) != ig.end()) continue;
if(dstb[i] == -1) {
dstb[i] = dstb[c] + 1;
q.push(i);
}
}
}
// if(a == 2 && b == 3) {
// cout<<"EEEE";
// for(int i:ig) cout<<i<<"< ";
// for(int i=0; i<n; i++){
// cout<<dsta[i]<<"|"<<dstb[i]<<" ";
// }
// cout<<"\n";
// }
set<int> as;
set<int> bs;
set<int> shared;
for(int i=0; i<n; i++) {
if(dsta[i] == dstb[i] && dsta[i] >= 0) shared.insert(i);
else if(dstb[i] > dsta[i]) as.insert(i);
else if(dsta[i] > dstb[i]) bs.insert(i);
}
return {as,bs,shared};
}
vector<int> path;
bool getpath(int x, int p, int g) {
if(p == -1){
path.clear();
ig.clear();
}
path.pb(x);
if(x == g) {
for(int i=0; i<path.size(); i++) {
ig.insert(path[i]);
}
return 1;
}
for(int i:adj[x]) {
if(i == p) continue;
if(getpath(i,x,g)) return 1;
}
path.pop_back();
return 0;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
if (fopen("input.in", "r")) {
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
}
int n, q;
cin>>n>>q;
for(int i=0; i<n+1; i++){
int u, v; cin>>u>>v;
adj[--u].pb(--v);
adj[v].pb(u);
}
dfs(0,0);
// step 1: erase edges
adj[dbs[0].f].erase(lower_bound(all(adj[dbs[0].f]), dbs[0].s));
adj[dbs[0].s].erase(lower_bound(all(adj[dbs[0].s]), dbs[0].f));
adj[dbs[1].f].erase(lower_bound(all(adj[dbs[1].f]), dbs[1].s));
adj[dbs[1].s].erase(lower_bound(all(adj[dbs[1].s]), dbs[1].f));
int l1 = dbs[0].f;
int r1 = dbs[0].s;
int l2 = dbs[1].f;
int r2 = dbs[1].s;
int qry[q][2];
for(int i=0; i<q; i++) {
cin>>qry[i][0]>>qry[i][1];
qry[i][0]--;
qry[i][1]--;
}
vector<int> ans(q,1);
vector<vector<int>> single = {
{l1,r1},
{r1,l1},
{l2,r2},
{r2,l2}
};
for(auto QRY : single){
vector<short> res(q,0);
int L = QRY[0];
int R = QRY[1];
auto JOINED = get_join(L,R,n);
// cout<<"JOIN: "<<L+1<<"|"<<R+1<<"\n";
// for(auto i:JOINED[0]) cout<<i<<" "; cout<<"\n";
// for(auto i:JOINED[1]) cout<<i<<" "; cout<<"\n";
// for(auto i:JOINED[2]) cout<<i<<" "; cout<<"\n";
for(int i=0; i<q; i++) {
int ra = -1;
int rb = -1;
if(JOINED[0].find(qry[i][0]) != JOINED[0].end()) ra = 1;
if(JOINED[1].find(qry[i][1]) != JOINED[1].end()) rb = 1;
if(JOINED[2].find(qry[i][0]) != JOINED[2].end()) ra = 0;
if(JOINED[2].find(qry[i][1]) != JOINED[2].end()) rb = 0;
if(ra + rb > 0) res[i] = 1;
}
for(int i=0; i<q; i++) ans[i]+=res[i];
// cout<<
// cout<<ans[0]<<" ";
// cout<<"#########"<<"\n";
}
// cout<<ans[0];
vector<vector<int>> dob = {
{l1, r1, l2, r2},
{l1, r1, r2, l2},
{r1, l1, l2, r2},
{r1, l1, r2, l2},
{l2, r2, l1, r1},
{l2, r2, r1, l1},
{r2, l2, l1, r1},
{r2, l2, r1, l1}
};
for(auto QRY : dob) {
vector<short> res(q, 0);
int L1 = QRY[0];
int R1 = QRY[1];
int L2 = QRY[2];
int R2 = QRY[3];
// cout<<"SEQ: "<<L1+1<<"|"<<R1+1<<"|"<<L2+1<<"|"<<R2+1<<"\n";
getpath(R1, -1, L2);
// for(auto p:ig) cout<<p.f+1<<"|"<<p.s+1<<" ";
// cout<<"\n";
int L = L1;
int R = R2;
auto JOINED = get_join(L,R,n);
// cout<<"JOIN: "<<L+1<<"|"<<R+1<<"\n";
// for(auto i:JOINED[0]) cout<<i<<" "; cout<<"\n";
// for(auto i:JOINED[1]) cout<<i<<" "; cout<<"\n";
// for(auto i:JOINED[2]) cout<<i<<" "; cout<<"\n";
for(int i=0; i<q; i++) {
int ra = -1;
int rb = -1;
if(JOINED[0].find(qry[i][0]) != JOINED[0].end()) ra = 1;
if(JOINED[1].find(qry[i][1]) != JOINED[1].end()) rb = 1;
if(JOINED[2].find(qry[i][0]) != JOINED[2].end()) ra = 0;
if(JOINED[2].find(qry[i][1]) != JOINED[2].end()) rb = 0;
// cout<<ra<<" "<<rb<<"< \n";
if(ra + rb > 0) res[i] = 1;
// cout<<res[i];
}
for(int i=0; i<q; i++) ans[i]+=res[i];
// cout<<ans[0];
}
for(int i:ans) cout<<i<<"\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4756kb
input:
4 6 1 2 1 3 1 4 2 3 2 4 1 2 1 3 1 4 2 3 2 4 3 4
output:
3 3 3 3 3 4
result:
ok 6 lines
Test #2:
score: 0
Accepted
time: 1ms
memory: 5016kb
input:
6 4 1 2 1 3 1 6 2 3 3 4 3 5 4 5 1 2 1 3 1 4 1 6
output:
2 2 4 1
result:
ok 4 lines
Test #3:
score: -100
Wrong Answer
time: 81ms
memory: 10924kb
input:
50000 50000 11561 23122 14261 28523 24407 48814 17947 35894 14803 29607 19557 39115 12415 24830 9583 19167 18397 36794 439 878 18040 36080 17150 34300 7922 15845 18938 37877 18625 37250 6459 12919 9818 19636 3579 7158 21323 42646 23882 47764 13603 27207 8353 16707 15907 31814 20935 41871 11686 23372...
output:
1 1 1 1 1 1 1 3 1 1 1 1 3 1 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 3 1 1 1 1 1 1 1 1 1 1 1 3 1 3 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 3 1 1 1 1 1 1 3 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 ...
result:
wrong answer 1st lines differ - expected: '4', found: '1'