图的邻接表表示中删除冗余边

#include
#include


using namespace std;


const int maxn = 1000 + 10;
struct Node {
int to;
};


vector edge[maxn], edgep[maxn];




int main() {
freopen("input.txt", "r", stdin);


int m, n;


cin >> n >> m;
for (int i = 0; i < m; i++) {
int t2;
Node t;
cin >> t2 >> t.to;
edge[t2].push_back(t);
}

for (int i = 1; i <= n; i++) {
int vis[maxn] = { 0 };
for (vector::iterator j = edge[i].begin(); j != edge[i].end(); j++) {
Node t = *j;
cout << i << " " << t.to << endl;

if (vis[t.to] == 0) {
edgep[i].push_back(t);
vis[t.to] = 1;
}
}
}
cout << endl;
for (int i = 1; i <= n; i++) {
for (vector::iterator j = edgep[i].begin(); j != edgep[i].end(); j++) {
Node t = *j;
cout << i << " " << t.to << endl;
}
}


return 0;
}

你可能感兴趣的:(基础算法练习)