给出样例
CASE1:
2 2
1 2
1 2
CASE2:
4 4
3 1
4 3
2 4
2 2
不得不说我之前还没看懂这个是什么意思,以为是重新加载了vector的clear,但是后来仔细想想才发现是当你需要将这个内存释放时才会用到这个,而不是一开始就需要定义进去(蒟蒻哭泣)。
所以只需要在代码中在要释放内存的地方加入
vector <type> x;
v.swap(x);
这两句语句即可
#include <iostream> #include <vector> using namespace std; int main() { int n, m; cin >> n >> m; vector<int> v[n+1]; for (int i = 1; i <= n; i++) v[i].push_back(i); for (int i = 0; i < m; i++) { int tmp1, tmp2; cin >> tmp1 >> tmp2; if (tmp2 == tmp1) continue; for (int j = 0; j < v[tmp2].size(); j++) v[tmp1].push_back(v[tmp2][j]); vector <int>x; v[tmp2].swap(x); } for (int i = 1; i <= n; i++) { for (int j = 0; j < v[i].size(); j++) { if (j == v[i].size()-1) cout << v[i][j]; else cout << v[i][j] << " "; } cout << endl; } return 0; }