pastebin

Paste #BAl -- näytä pelkkänä tekstinä -- uusi tämän pohjalta

Värjäys: Tyyli: ensimmäinen rivinumero: Tabin korvaus:

  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
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <bits/stdc++.h>
#define F first
#define S second
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef long double ld;
struct LPSolver {
    const ld eps=1e-9;
    const ld inf=1e30;
    int m, n;
    vector<int> N, B;
    vector<vector<ld> > D;
    LPSolver(vector<vector<ld> >& A, vector<ld>& b, vector<ld>& c) :
    m(b.size()), n(c.size()), N(n + 1), B(m), D(m + 2, vector<ld>(n + 2)) {
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) D[i][j] = A[i][j];}
        for (int i = 0; i < m; i++) { 
            B[i] = n + i; D[i][n] = -1; D[i][n + 1] = b[i];}
        for (int j = 0; j < n; j++) { N[j] = j; D[m][j] = -c[j]; }
        N[n] = -1; D[m + 1][n] = 1;
    }
    void Pivot(int r, int s) {
        ld inv = 1.0 / D[r][s];
        for (int i = 0; i < m + 2; i++) if (i != r)
            for (int j = 0; j < n + 2; j++) if (j != s)
                D[i][j] -= D[r][j] * D[i][s] * inv;
        for (int j = 0; j < n + 2; j++) if (j != s) D[r][j] *= inv;
        for (int i = 0; i < m + 2; i++) if (i != r) D[i][s] *= -inv;
        D[r][s] = inv;
        swap(B[r], N[s]);
    }
    bool Simplex(int phase) {
        int x = phase == 1 ? m + 1 : m;
        while (true) {
            int s = -1;
            for (int j = 0; j <= n; j++) {
                if (phase == 2 && N[j] == -1) continue;
                if (s==-1||D[x][j]<D[x][s]||(D[x][j]==D[x][s]&&N[j]<N[s])) s=j;
            }
            if (D[x][s] > -eps) return true;
            int r = -1;
            for (int i = 0; i < m; i++) {
                if (D[i][s] < eps) continue;
                if (r == -1 || D[i][n + 1] / D[i][s] < D[r][n + 1] / D[r][s] ||
                    ((D[i][n + 1]/D[i][s])==(D[r][n+1]/D[r][s])&&B[i]<B[r]))r=i;
            }
            if (r == -1) return false;
            Pivot(r, s);
        }
    }
    ld Solve(vector<ld>& x) {
        int r = 0;
        for (int i = 1; i < m; i++) if (D[i][n + 1] < D[r][n + 1]) r = i;
        if (D[r][n + 1] < -eps) {
            Pivot(r, n);
            if (!Simplex(1) || D[m + 1][n + 1] < -eps) return -inf;
            for (int i = 0; i < m; i++) if (B[i] == -1) {
                int s = -1;
                for (int j = 0; j <= n; j++)
                if (s==-1||D[i][j]<D[i][s]||(D[i][j]==D[i][s]&&N[j]<N[s])) s=j;
                Pivot(i, s);
            }
        }
        if (!Simplex(2)) return inf;
        x = vector<ld>(n);
        for (int i = 0; i < m; i++) if (B[i] < n) x[B[i]] = D[i][n + 1];
        return D[m][n + 1];
    }
};
 
vector<vector<ld> > lpA;
vector<ld> lpb;
 
void addC(vector<int> t, int s, int v){
    vector<ld> r(400);
    for (int a:t){
        if (s==1) r[a-1]=1;
        else r[a-1]=-1;
    }
    lpA.push_back(r);
    if (s==1) lpb.push_back(v);
    else lpb.push_back(-v);
}
 
vector<int> g[444];
int p[444];
vector<int> vs[444];
void dfs(int x, int d, int k){
    vs[d].push_back(x);
    if (d==k){
        vector<int> c;
        while (d>0){
            c.push_back(x);
            x=p[x];
            d--;
        }
        addC(c, -1, 1);
        return;
    }
    for (int nx:g[x]){
        if (nx!=p[x]){
            p[nx]=x;
            dfs(nx, d+1, k);
        }
    }
}
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n,k;
    cin>>n>>k;
    if (k*k>n) {
        cout<<"DA"<<endl;
        return 0;
    }
    for (int i=0;i<n-1;i++){
        int a,b;
        cin>>a>>b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    dfs(1, 0, k);
    for (int i=1;i<=k;i++){
        addC(vs[i], 1, 1);
    }
    vector<ld> lpc(400);
    for (int i=0;i<400;i++){
        lpc[i]=1;
    }
    LPSolver lp(lpA, lpb, lpc);
    vector<ld> ans;
    ld val=lp.Solve(ans);
    if (val>0){
        cout<<"DA"<<endl;
    }
    else{
        cout<<"NE"<<endl;
    }
}