把这份代码转换成c++代码var n,i,j,p,x,min,tot,t,len:longint; 2 out_,in_,a,heap:array[0..30005] of longint; 3 son,nxt:array[0..1000005] of longint; 4 lnk:array[0..30005] of longint; 5 procedure print_no; 6 begin 7 writeln('no solution'); 8 close(input); close(output); 9 halt; 10 end; 11 procedure put(id:longint); 12 var i:longint; 13 begin 14 inc(len); heap[len]:=id; i:=len; 15 while (i>1) do 16 begin 17 if (heap[i>>1]>heap[i]) then 18 begin 19 heap[0]:=heap[i]; heap[i]:=heap[i>>1]; heap[i>>1]:=heap[0]; 20 i:=i>>1; 21 end 22 else break; 23 end; 24 end; 25 function get:longint; 26 var fa,son:longint; 27 begin 28 get:=heap[1]; heap[1]:=heap[len]; dec(len); fa:=1; 29 while (fa<<1<=len) do 30 begin 31 if (fa<<1+1>len) or (heap[fa<<1]<heap[fa<<1+1]) then son:=fa*2 32 else son:=fa*2+1; 33 if heap[fa]>heap[son] then 34 begin 35 heap[0]:=heap[fa]; heap[fa]:=heap[son]; heap[son]:=heap[0]; 36 fa:=son; 37 end 38 else break; 39 end; 40 end; 41 procedure add(x,y:longint); 42 begin 43 inc(tot); son[tot]:=y; nxt[tot]:=lnk[x]; lnk[x]:=tot; 44 end; 45 begin 46 readln(n); 47 for i:=1 to n do 48 begin 49 read(out_[i]); 50 for j:=1 to out_[i] do 51 begin 52 read(x); inc(in_[x]); add(i,x); 53 end; 54 end; 55 min:=maxlongint; 56 for i:=1 to n do 57 if (in_[i]=0) then begin min:=0; put(i); end; 58 if min<>0 then print_no; 59 repeat 60 p:=get; inc(t); a[t]:=p; j:=lnk[p]; 61 in_[p]:=-1; 62 while j<>0 do 63 begin 64 dec(in_[son[j]]); 65 if in_[son[j]]=0 then put(son[j]); 66 j:=nxt[j]; 67 end; 68 until len=0; 69 writeln(t); 70 for i:=1 to t do write(a[i],' '); 71 end.
时间: 2024-01-20 22:02:51 浏览: 74
c C++算法大全详解(付代码)
5星 · 资源好评率100%
以下是将给定的代码转换成 C++ 代码的结果:
```c++
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 30005;
const int MAXM = 1000005;
int n, len, tot, t, min, out_[MAXN], in_[MAXN], a[MAXN], heap[MAXN];
int son[MAXM], nxt[MAXM], lnk[MAXN];
void print_no() {
cout << "no solution" << endl;
exit(0);
}
void put(int id) {
++len;
heap[len] = id;
int i = len;
while (i > 1) {
if (heap[i >> 1] > heap[i]) {
swap(heap[i], heap[i >> 1]);
i >>= 1;
} else {
break;
}
}
}
int get() {
int fa = 1, son;
int res = heap[1];
heap[1] = heap[len];
--len;
while ((fa << 1) <= len) {
if ((fa << 1) + 1 > len || heap[fa << 1] < heap[(fa << 1) + 1]) {
son = fa << 1;
} else {
son = (fa << 1) + 1;
}
if (heap[fa] > heap[son]) {
swap(heap[fa], heap[son]);
fa = son;
} else {
break;
}
}
return res;
}
void add(int x, int y) {
++tot;
son[tot] = y;
nxt[tot] = lnk[x];
lnk[x] = tot;
}
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> out_[i];
for (int j = 1; j <= out_[i]; ++j) {
int x;
cin >> x;
++in_[x];
add(i, x);
}
}
min = INT_MAX;
for (int i = 1; i <= n; ++i) {
if (in_[i] == 0) {
min = 0;
put(i);
}
}
if (min != 0) {
print_no();
}
while (len > 0) {
int p = get();
a[++t] = p;
int j = lnk[p];
in_[p] = -1;
while (j != 0) {
--in_[son[j]];
if (in_[son[j]] == 0) {
put(son[j]);
}
j = nxt[j];
}
}
cout << t << endl;
for (int i = 1; i <= t; ++i) {
cout << a[i] << " ";
}
cout << endl;
return 0;
}
```
阅读全文