luyencode / comments

Server lưu trữ bình luận trên Luyện Code
https://luyencode.net
6 stars 3 forks source link

https://oj.luyencode.net/problem/COMPCONN #753

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

Chi tiết bài tập - Luyện Code Online

https://luyencode.net/problem/COMPCONN

chatctt commented 2 years ago

Kiệt code dạo.

Xem code AC

#include #include #include #include using namespace std; int n,m,a[2000][2000],visited[2000]; vector< vector > res; void bfs(int u) { queueq; vectorv; q.push(u); visited[u]=1; while(!q.empty()) { int tmp=q.front(); q.pop(); v.push_back(tmp); for(int i=1;i<=n;i++) { if(a[tmp][i]&&visited[i]==0) { q.push(i); visited[i]=1; } } } res.push_back(v); } int main() { cin>>n>>m; for(int i=1;i<=m;i++) { int x,y; cin>>x>>y; a[x][y]=a[y][x]=1; } for(int i=1;i<=n;i++) { if(visited[i]==0) bfs(i); } cout<

bibimoni commented 2 years ago
View AC code

``` // https://oj.luyencode.net/problem/COMPCONN #include using namespace std; const int maxN = 10001; int n, m; vector adj[maxN]; bool visited[maxN]; int numberOfTPLT = 0; vector numberOfVertexes[maxN]; // luu so dinh cua tplt void inputting() { cin >> n >> m; for(int i = 0; i < m; i++) { int x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } fill_n(visited, n+1, false); } void BFS(int src) { numberOfTPLT++; queue Q; int vertexes = 1; // so dinh cua tplt Q.push(src); numberOfVertexes[numberOfTPLT].push_back(src); visited[src] = true; while(!Q.empty()) { int u = Q.front(); Q.pop(); for(auto x : adj[u]) { if(!visited[x]) { visited[x] = true; Q.push(x); numberOfVertexes[numberOfTPLT].push_back(x); } } } } void outputting() { cout << numberOfTPLT; // in ra so dinh cua tplt for(int i = 1; i <= numberOfTPLT; i++) { cout << "\n" << numberOfVertexes[i].size(); sort(numberOfVertexes[i].begin(), numberOfVertexes[i].end()); for(auto x: numberOfVertexes[i]) { cout << ' ' << x; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); inputting(); for(int i = 1; i <= n; i++) { if(!visited[i]) { BFS(i); } } outputting(); return 0; } ```

gnaigsolo commented 1 year ago

Đừng in ra khoảng trắng thừa nha các bạn :))