//#include<bits/stdc++.h>
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
const int P=1<<22;
const int N =2*1e3+2;
int n, k;
int tree[N][N];
vector<int>wina;
map<int, pair<int,int>> mapa;
map<pair<int,int>, int> taken;
int global;
vector<pair<int,int>>G[N][N];
void rekurencja(int x, pair<int,int>wier){
taken[wier] = x;
global++;
for (auto next:G[wier.first][wier.second]){
if(taken[next] != x) rekurencja (x, next);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin >> n >> k;
int licz=0;
for (int i=0; i<n; i++){
for (int j=0; j<i; j++){
cin >> tree[i][j];
wina.push_back(tree[i][j]);
mapa[licz] = {i,j};
licz++;
if(i-1>=0)G[i][j].push_back({i-1,j});
if (j!=i-1)G[i][j].push_back({i,j});
}
}
sort(wina.begin(), wina.end());
int obieg=1;
for (int i=0; i<wina.size();i++){
global = 0;
rekurencja(obieg,mapa[i]);
obieg++;
if (global <= k) {
cout << wina[i];
break;
}
}
}
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 | //#include<bits/stdc++.h> #include<iostream> #include<map> #include<vector> #include<algorithm> using namespace std; const int P=1<<22; const int N =2*1e3+2; int n, k; int tree[N][N]; vector<int>wina; map<int, pair<int,int>> mapa; map<pair<int,int>, int> taken; int global; vector<pair<int,int>>G[N][N]; void rekurencja(int x, pair<int,int>wier){ taken[wier] = x; global++; for (auto next:G[wier.first][wier.second]){ if(taken[next] != x) rekurencja (x, next); } } int main(){ ios_base::sync_with_stdio(0); cin >> n >> k; int licz=0; for (int i=0; i<n; i++){ for (int j=0; j<i; j++){ cin >> tree[i][j]; wina.push_back(tree[i][j]); mapa[licz] = {i,j}; licz++; if(i-1>=0)G[i][j].push_back({i-1,j}); if (j!=i-1)G[i][j].push_back({i,j}); } } sort(wina.begin(), wina.end()); int obieg=1; for (int i=0; i<wina.size();i++){ global = 0; rekurencja(obieg,mapa[i]); obieg++; if (global <= k) { cout << wina[i]; break; } } } |
English