Balanced Neighbors(图)

题目描述
You are given an integer N. Build an undirected graph with N vertices with indices 1 to N that satisfies the following two conditions:
·The graph is simple and connected.
·There exists an integer S such that, for every vertex, the sum of the indices of the vertices adjacent to that vertex is S.
It can be proved that at least one such graph exists under the constraints of this problem.

Constraints
·All values in input are integers.
·3≤N≤100

输入
Input is given from Standard Input in the following format:

N

输出
In the first line, print the number of edges, M, in the graph you made. In the i-th of the following M lines, print two integers ai and bi, representing the endpoints of the i-th edge.
The output will be judged correct if the graph satisfies the conditions.

样例输入
3

样例输出
2
1 3
2 3

提示
For every vertex, the sum of the indices of the vertices adjacent to that vertex is 3.

思路
先建立一个1到n的完全图,消边即可得到规律,当n为偶数时,总边数为n*(n-2)/2,当i+j == n+1时消边,当n为奇数时,总边数为(n-1)*(n-1)/2,当i+j ==n时消边。

代码实现

#include<bits/stdc++.h>
using namespace std;

const int N=105;
const int mod=1000000007;
typedef long long ll;

int n;

int main()
{
    scanf("%d",&n);
    if(n&1)
    {
        printf("%d\n",(n-1)*(n-1)/2);
        for(int i=1;i<n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                if((i+j)!=n) printf("%d %d\n",i,j);
            }
        }
    }
    else
    {
        printf("%d\n",n*(n-2)/2);
        for(int i=1;i<n;i++)
        {
            for(int j=i+1;j<=n;j++)
            {
                if((i+j)!=(n+1)) printf("%d %d\n",i,j);
            }
        }
    }
    return 0;
}

import numpy as np import pandas as pd from sklearn.model_selection import StratifiedKFold from sklearn.preprocessing import StandardScaler from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, roc_auc_score from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier from xgboost import XGBClassifier from imblearn.pipeline import Pipeline from imblearn.over_sampling import SMOTE from imblearn.under_sampling import TomekLinks from sklearn.decomposition import PCA from sklearn.feature_selection import SelectKBest, mutual_info_classif, VarianceThreshold from sklearn.tree import DecisionTreeClassifier from sklearn.feature_selection import RFE from sklearn.svm import SVC df = pd.read_excel(r'C:\Users\14576\Desktop\计算机资料\石波-乳腺癌\Traintest1.xlsx') data = np.array(df) X = data[:, 1:] y = data[:, 0] pipeline = Pipeline([ ('resample', SMOTE(sampling_strategy=0.8,k_neighbors=7,random_state=42)), # 过采样在前 ('clean', TomekLinks(sampling_strategy='majority')), # 欠采样在后 ('scaler', StandardScaler()), #('clean', TomekLinks(sampling_strategy='majority')), # 欠采样在后 ('variance_threshold', VarianceThreshold(threshold=0.15)), ('pca', PCA(n_components=0.90)), ('rfe', RFE(estimator=XGBClassifier(max_depth=3, n_estimators=200), n_features_to_select=10)), ('model', AdaBoostClassifier( n_estimators=1100, learning_rate=0.1, estimator=DecisionTreeClassifier(max_depth=2), random_state=42 )) # 模型最后 ]) #'resample', SMOTE(sampling_strategy=0.7,k_neighbors=5,random_state=42) kf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42) metrics = { 'Accuracy': [], 'Precision': [], 'Recall': [], 'F1': [], 'AUC': [] } for train_idx, val_idx in kf.split(X, y): X_train, X_val = X[train_idx], X[val_idx] y_train, y_val = y[train_idx], y[val_idx] # 训练并预测 pipeline.fit(X_train, y_train) y_pred = pipeline.predict(X_val) y_proba = pipeline.predict_proba(X_val)[:, 1] # 记录指标 metrics['Accuracy'].append(accuracy_score(y_val, y_pred)) metrics['Precision'].append(precision_score(y_val, y_pred)) metrics['Recall'].append(recall_score(y_val, y_pred)) metrics['F1'].append(f1_score(y_val, y_pred)) metrics['AUC'].append(roc_auc_score(y_val, y_proba)) for metric, values in metrics.items(): print(f"{metric}: {np.mean(values):.4f} ")优化提升模型各项指标
最新发布
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值