题目

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;
struct Color{
int r, g, b;
bool operator<(const Color &x)const{
return r < x.r && g < x.g && b < x.b;
}
}c[maxn];
vector<int> G[maxn];
int ind[maxn], dep[maxn];
int main(){
int n, i, j;
cin >> n;
for(i = 1; i <= n; i++){
cin >> c[i].r >> c[i].g >> c[i].b;
}
if(c[1] < c[2] || c[2] < c[1]){
cout << -1;
return 0;
}
for(i = 3; i <= n; i++){
for(j = i + 1; j <= n; j++){
if(c[i] < c[j]){
G[i].push_back(j);
ind[j]++;
}
else if(c[j] < c[i]){
G[j].push_back(i);
ind[i]++;
}
}
}
for(i = 3; i <= n; i++){//1和2合并成一个点
if(c[1] < c[i] || c[2] < c[i]){
G[2].push_back(i);
ind[i]++;
}
else if(c[i] < c[1] || c[i] < c[2]){
G[i].push_back(2);
ind[2]++;
}
}
queue<int> q;
for(i = 2; i <= n; i++){
if(!ind[i]) q.push(i);
}
while(!q.empty()){
int u = q.front();
q.pop();
for(auto v : G[u]){
ind[v]--;
if(!ind[v]){
q.push(v);
dep[v] = dep[u] + 1;
}
}
}
cout << dep[2] << '\n';
for(i = 2; i <= n; i++){
cout << dep[i] << '\n';
}
return 0;
}