QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#684814 | #2437. Wireless is the New Fiber | LJY_ljy | AC ✓ | 8ms | 5644kb | C++11 | 1.8kb | 2024-10-28 16:02:56 | 2024-10-28 16:02:57 |
Judging History
answer
//本题本质:已知一棵树所有节点的度数,求画出该树。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 1e4 + 10;
vector<int> G[MAXN];
int n, m, sum[MAXN];
struct node {
int deg, index;
bool operator < (const node &a) const {
return deg < a.deg;
}
} a[MAXN];
int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int main() {
n = read(); m = read();
for (int i = 1; i <= m; i++) {
int u = read(), v = read();
G[u].push_back(v + 1);
G[v].push_back(u + 1);
a[u + 1].deg++;
a[v + 1].deg++;
a[u + 1].index = u + 1;
a[v + 1].index = v + 1;
}
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; i++)
sum[i] = sum[i - 1] + a[i].deg;
int j = -1;
for (int i = n; i >= 0; i--) {
if (sum[i] + (n - i) * 1 <= 2 * (n - 1)) {
j = i;
break;
}
}
for (int i = 1; i <= n; i++) {
if (i == j + 1)
a[i].deg = 2 * (n - 1) - sum[j] - (n - j - 1);
if (i > j + 1)
a[i].deg = 1;
}
printf("%d\n%d %d\n", n - j, n, n - 1);
sort(a + 1, a + 1 + n);
queue<int> q1, q2;
for (int i = 1; i <= n; i++) {
if (a[i].deg == 1) q1.push(i);
else q2.push(i);
}
while (!q1.empty() && !q2.empty()) {
int t = q1.front();
q1.pop();
int x = q2.front();
printf("%d %d\n", a[t].index - 1, a[x].index - 1);
a[x].deg--;
if (a[x].deg == 1) {
q2.pop();
q1.push(x);
}
}
while (!q1.empty()) {
int t = q1.front();
q1.pop();
int x = q1.front();
q1.pop();
printf("%d %d\n", a[t].index - 1, a[x].index - 1);
}
return 0;
}
Details
Test #1:
score: 100
Accepted
time: 1ms
memory: 4040kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 4040kb
Test #3:
score: 0
Accepted
time: 0ms
memory: 3988kb
Test #4:
score: 0
Accepted
time: 0ms
memory: 4248kb
Test #5:
score: 0
Accepted
time: 0ms
memory: 4020kb
Test #6:
score: 0
Accepted
time: 0ms
memory: 4240kb
Test #7:
score: 0
Accepted
time: 0ms
memory: 4276kb
Test #8:
score: 0
Accepted
time: 0ms
memory: 4048kb
Test #9:
score: 0
Accepted
time: 1ms
memory: 4360kb
Test #10:
score: 0
Accepted
time: 0ms
memory: 3984kb
Test #11:
score: 0
Accepted
time: 0ms
memory: 4008kb
Test #12:
score: 0
Accepted
time: 0ms
memory: 4416kb
Test #13:
score: 0
Accepted
time: 1ms
memory: 4024kb
Test #14:
score: 0
Accepted
time: 1ms
memory: 4036kb
Test #15:
score: 0
Accepted
time: 0ms
memory: 4092kb
Test #16:
score: 0
Accepted
time: 1ms
memory: 4048kb
Test #17:
score: 0
Accepted
time: 1ms
memory: 4464kb
Test #18:
score: 0
Accepted
time: 4ms
memory: 5392kb
Test #19:
score: 0
Accepted
time: 0ms
memory: 4508kb
Test #20:
score: 0
Accepted
time: 0ms
memory: 4824kb
Test #21:
score: 0
Accepted
time: 3ms
memory: 4568kb
Test #22:
score: 0
Accepted
time: 4ms
memory: 4720kb
Test #23:
score: 0
Accepted
time: 2ms
memory: 4788kb
Test #24:
score: 0
Accepted
time: 3ms
memory: 5352kb
Test #25:
score: 0
Accepted
time: 7ms
memory: 5184kb
Test #26:
score: 0
Accepted
time: 7ms
memory: 5276kb
Test #27:
score: 0
Accepted
time: 8ms
memory: 5644kb
Test #28:
score: 0
Accepted
time: 4ms
memory: 5588kb
Test #29:
score: 0
Accepted
time: 4ms
memory: 5584kb
Test #30:
score: 0
Accepted
time: 0ms
memory: 4012kb
Test #31:
score: 0
Accepted
time: 0ms
memory: 4040kb
Test #32:
score: 0
Accepted
time: 0ms
memory: 3996kb
Test #33:
score: 0
Accepted
time: 2ms
memory: 4420kb
Test #34:
score: 0
Accepted
time: 0ms
memory: 4020kb
Test #35:
score: 0
Accepted
time: 0ms
memory: 4044kb
Test #36:
score: 0
Accepted
time: 0ms
memory: 4276kb
Test #37:
score: 0
Accepted
time: 0ms
memory: 3984kb