답안 #1030535

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1030535 2024-07-22T06:33:49 Z 12345678 Graph (BOI20_graph) C++17
0 / 100
1 ms 2652 KB
#include <bits/stdc++.h>

using namespace std;

const int nx=1e5+5;

int n, m, u, v, w, a[nx], b[nx], f, vs[nx];
double res[nx];
vector<tuple<int, int, int>> d[nx];
vector<int> comp;
set<double> s;

void dfs(int u, int p)
{
    vs[u]=1;
    comp.push_back(u);
    for (auto [v, w, idx]:d[u])
    {
        if (idx==p) continue;
        if (vs[v])
        {
            int sma=a[u]+a[v], smb=b[u]+b[v];
            if (sma==0&&b[u]+b[v]==w) continue;
            else if (sma==0&&b[u]+b[v]!=w)
            {
                f=1;
                break;
            }
            s.insert(((double)(w-smb))/((double)sma));
        }
        else
        {
            a[v]=-a[u];
            b[v]=w-b[u];
            dfs(v, idx);
        }
    }
}
int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n>>m;
    for (int i=1; i<=m; i++) cin>>u>>v>>w, d[u].push_back({v, w, i}), d[v].push_back({u, w, i});
    for (int i=1; i<=n; i++)
    {
        if (!vs[i])
        {
            comp.clear();
            s.clear();
            a[i]=1;
            dfs(i, -1);
            if (s.size()>1) f=1;
            else if (s.size()==1)
            {
                double vl=*s.begin();
                for (auto x:comp) res[x]=a[x]*vl+b[x];
            }
            else
            {
                vector<double> v;
                for (auto x:comp) if (a[x]!=0) v.push_back((double)(-b[x])/(double)a[x]);
                sort(v.begin(), v.end());
                cout<<"v ";
                for (auto x:v) cout<<x<<' ';
                cout<<'\n';
                int l=0, r=(int)v.size()-1;
                while (l<r)
                {
                    int md=(l+r+1)/2;
                    //cout<<"md "<<md<<'\n';
                    double sm1=0, sm2=0;
                    for (auto x:comp) sm1+=abs((double)a[x]*v[md]+(double)b[x]), sm2+=abs((double)a[x]*v[md-1]+(double)b[x]);
                    //cout<<"sm1 "<<sm1<<' '<<"sm2 "<<sm2<<'\n';
                    if (sm1<sm2) l=md;
                    else r=md-1;
                }
                //cout<<"debug "<<l<<' '<<v[l]<<'\n';
                double t=0;
                if (v.empty()) t=0;
                else t=v[l];
                //cout<<"mn "<<mn.second<<'\n';
                //if (t!=mn.second) cout<<1/0;
                for (auto x:comp) res[x]=t*a[x]+b[x];
            }
        }
    }
    if (f) return cout<<"NO\n", 0;
    cout<<"YES\n";
    for (int i=1; i<=n; i++) cout<<setprecision(8)<<res[i]<<' ';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB answer = YES
2 Incorrect 1 ms 2652 KB Expected YES or NO
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB answer = YES
2 Incorrect 1 ms 2652 KB Expected YES or NO
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB answer = YES
2 Incorrect 1 ms 2652 KB Expected YES or NO
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB answer = YES
2 Incorrect 1 ms 2652 KB Expected YES or NO
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2652 KB answer = YES
2 Incorrect 1 ms 2652 KB Expected YES or NO
3 Halted 0 ms 0 KB -