#include <bits/stdc++.h> using namespace std; class Solution { public: void dfs(int node, vector<int> graph[], vector<bool>& visited){ if(visited[node]) return; visited[node] = true; for(int i = 0; i < graph[node].size(); i++){ dfs(graph[node][i], graph, visited); } } int countComponents(int n, vector< vector<int> >& edges) { vector <bool> visited(n); if(!n) return 0; vector <int> graph[n]; for(int i = 0; i < edges.size(); i++){ int u = edges[i][0]; int v = edges[i][1]; graph[u].push_back(v); graph[v].push_back(u); } int ret = 0; for(int i = 0; i < n; i++){ if(!visited[i]){ dfs(i, graph, visited); ret++; } } return ret; } }; int main(){ Solution ob; int N,K; cin>>N>>K; int tab[2][1000]; int L[2000]; for(int i=0; i<2000; ++i) L[i] = 0; for(int i=0; i<2; ++i) for(int j=0; j<N; ++j) cin>>tab[i][j]; for(int k=1; k<=2*N; ++k) for(int l=k; l<=2*N; ++l) { int W; vector< vector<int> > v; W = l - k + 1; for(int i=0; i<2; ++i) for(int j=0; j<N; ++j) { int p1,p2,p3,p4; p1 = tab[i][j]; p2 = tab[(i+1) % 2][j]; p3 = tab[i][(j+1) % N]; p4 = tab[i][(j-1+N) % N]; if(p1 >= k && p1 <= l) { if(p2 >= k && p2 <= l) { v.push_back({p1-k,p2-k}); } if(p3 >= k && p3 <= l) { v.push_back({p1-k,p3-k}); } if(p4 >= k && p4 <= l) { v.push_back({p1-k,p4-k}); } } } int ans; ans = ob.countComponents(W, v); //cout<<k<< " "<<l<<" "<<ans<<endl; L[ans]++; } for(int i=1; i<=K; ++i) cout<<L[i]<<" "; cout<<endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #include <bits/stdc++.h> using namespace std; class Solution { public: void dfs(int node, vector<int> graph[], vector<bool>& visited){ if(visited[node]) return; visited[node] = true; for(int i = 0; i < graph[node].size(); i++){ dfs(graph[node][i], graph, visited); } } int countComponents(int n, vector< vector<int> >& edges) { vector <bool> visited(n); if(!n) return 0; vector <int> graph[n]; for(int i = 0; i < edges.size(); i++){ int u = edges[i][0]; int v = edges[i][1]; graph[u].push_back(v); graph[v].push_back(u); } int ret = 0; for(int i = 0; i < n; i++){ if(!visited[i]){ dfs(i, graph, visited); ret++; } } return ret; } }; int main(){ Solution ob; int N,K; cin>>N>>K; int tab[2][1000]; int L[2000]; for(int i=0; i<2000; ++i) L[i] = 0; for(int i=0; i<2; ++i) for(int j=0; j<N; ++j) cin>>tab[i][j]; for(int k=1; k<=2*N; ++k) for(int l=k; l<=2*N; ++l) { int W; vector< vector<int> > v; W = l - k + 1; for(int i=0; i<2; ++i) for(int j=0; j<N; ++j) { int p1,p2,p3,p4; p1 = tab[i][j]; p2 = tab[(i+1) % 2][j]; p3 = tab[i][(j+1) % N]; p4 = tab[i][(j-1+N) % N]; if(p1 >= k && p1 <= l) { if(p2 >= k && p2 <= l) { v.push_back({p1-k,p2-k}); } if(p3 >= k && p3 <= l) { v.push_back({p1-k,p3-k}); } if(p4 >= k && p4 <= l) { v.push_back({p1-k,p4-k}); } } } int ans; ans = ob.countComponents(W, v); //cout<<k<< " "<<l<<" "<<ans<<endl; L[ans]++; } for(int i=1; i<=K; ++i) cout<<L[i]<<" "; cout<<endl; return 0; } |