社区讨论
一个疑问
P7771【模板】欧拉路径参与者 2已保存回复 4
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 4 条
- 当前快照
- 1 份
- 快照标识符
- @m238emcg
- 此快照首次捕获于
- 2024/10/10 19:44 去年
- 此快照最后确认于
- 2025/11/04 17:30 4 个月前
100pts:
CPP#include<bits/stdc++.h>
using namespace std;
int n, m, s1, s2, h[100005];
int cntin[100005], cntout[100005], num;
bool f, vis[100005];
vector<int> e[100005];
stack<int> st;
void DFS(int u, int d) {
for(int i = h[u]; i < (int)e[u].size(); i = h[u]) {
int v = e[u][i];
h[u] = i + 1;
DFS(v, d + 1);
}
st.push(u);
}
int main() {
scanf("%d %d", &n, &m);
for(int i = 1; i <= m; i++) {
scanf("%d %d", &s1, &s2);
e[s1].push_back(s2);
cntout[s1]++;
cntin[s2]++;
}
for(int i = 1; i <= n; i++) {
num += (cntin[i] + cntout[i]) % 2 == 0 ? 0 : 1;
}
f = true;
if(num == 2) {
for(int i = 1; i <= n; i++) {
sort(e[i].begin(), e[i].end());
}
for(int i = 1; i <= n; i++) {
if(cntout[i] - cntin[i] > 0 && (cntout[i] - cntin[i]) % 2 == 1) {
DFS(i, 0);
break;
}
}
if(f) {
while(!st.empty()) {
printf("%d ", st.top());
st.pop();
}
return 0;
}
} else if(num == 0) {
for(int i = 1; i <= n; i++) {
sort(e[i].begin(), e[i].end());
}
DFS(1, 0);
if(f) {
while(!st.empty()) {
printf("%d ", st.top());
st.pop();
}
return 0;
}
}
printf("No");
return 0;
}
20pts:
CPP#include<bits/stdc++.h>
using namespace std;
int n, m, s1, s2, h[100005];
int cntin[100005], cntout[100005], num;
bool f, vis[100005];
vector<int> e[100005];
stack<int> st;
void DFS(int u, int d) {
for(int i = h[u]; i < (int)e[u].size(); i++) {
int v = e[u][i];
h[u] = i + 1;
DFS(v, d + 1);
}
st.push(u);
}
int main() {
scanf("%d %d", &n, &m);
for(int i = 1; i <= m; i++) {
scanf("%d %d", &s1, &s2);
e[s1].push_back(s2);
cntout[s1]++;
cntin[s2]++;
}
for(int i = 1; i <= n; i++) {
num += (cntin[i] + cntout[i]) % 2 == 0 ? 0 : 1;
}
f = true;
if(num == 2) {
for(int i = 1; i <= n; i++) {
sort(e[i].begin(), e[i].end());
}
for(int i = 1; i <= n; i++) {
if(cntout[i] - cntin[i] > 0 && (cntout[i] - cntin[i]) % 2 == 1) {
DFS(i, 0);
break;
}
}
if(f) {
while(!st.empty()) {
printf("%d ", st.top());
st.pop();
}
return 0;
}
} else if(num == 0) {
for(int i = 1; i <= n; i++) {
sort(e[i].begin(), e[i].end());
}
DFS(1, 0);
if(f) {
while(!st.empty()) {
printf("%d ", st.top());
st.pop();
}
return 0;
}
}
printf("No");
return 0;
}
显然只有DFS里的for循环的i=h[u]和i++有区别,为什么要写i=h[u]而另一种却过不了?
回复
共 4 条回复,欢迎继续交流。
正在加载回复...