Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], skipTags: ["script","noscript","style","textarea","code"], processEscapes: true }});

Problem F
Pizza Delivery

Alyssa is a college student, living in New Tsukuba City. All the streets in the city are one-way. A new social experiment starting tomorrow is on alternative traffic regulation reversing the one-way directions of street sections. Reversals will be on one single street section between two adjacent intersections for each day; the directions of all the other sections will not change, and the reversal will be canceled on the next day.

Alyssa orders a piece of pizza everyday from the same pizzeria. The pizza is delivered along the shortest route from the intersection with the pizzeria to the intersection with Alyssa's house.

Altering the traffic regulation may change the shortest route. Please tell Alyssa how the social experiment will affect the pizza delivery route.

Input

The input consists of a single test case in the following format.

$n$ $m$
$a_1$ $b_1$ $c_1$
...
$a_m$ $b_m$ $c_m$

The first line contains two integers, $n$, the number of intersections, and $m$, the number of street sections in New Tsukuba City ($2 \leq n \leq 100 000, 1 \leq m \leq 100 000$). The intersections are numbered $1$ through $n$ and the street sections are numbered $1$ through $m$.

The following $m$ lines contain the information about the street sections, each with three integers $a_i$, $b_i$, and $c_i$ ($1 \leq a_i n, 1 \leq b_i \leq n, a_i \ne b_i, 1 \leq c_i \leq 100 000$). They mean that the street section numbered $i$ connects two intersections with the one-way direction from $a_i$ to $b_i$, which will be reversed on the $i$-th day. The street section has the length of $c_i$. Note that there may be more than one street section connecting the same pair of intersections.

The pizzeria is on the intersection 1 and Alyssa's house is on the intersection 2. It is guaranteed that at least one route exists from the pizzeria to Alyssa's before the social experiment starts.

Output

The output should contain $m$ lines. The $i$-th line should be

HAPPY if the shortest route on the $i$-th day will become shorter,

SOSO if the length of the shortest route on the $i$-th day will not change, and

SAD if the shortest route on the $i$-th day will be longer or if there will be no route from the pizzeria to Alyssa's house.

Alyssa doesn't mind whether the delivery bike can go back to the pizzeria or not.

Sample Input 1

4 5
1 3 5
3 4 6
4 2 7
2 1 18
2 3 12

Sample Output 1

SAD
SAD
SAD
SOSO
HAPPY

Sample Input 2

7 5
1 3 2
1 6 3
4 2 4
6 2 5
7 5 6

Sample Output 2

SOSO
SAD
SOSO
SAD
SOSO

Sample Input 3

10 14
1 7 9
1 8 3
2 8 4
2 6 11
3 7 8
3 4 4
3 2 1
3 2 7
4 8 4
5 6 11
5 8 12
6 10 6
7 10 8
8 3 6

Sample Output 3

SOSO
SAD
HAPPY
SOSO
SOSO
SOSO
SAD
SOSO
SOSO
SOSO
SOSO
SOSO
SOSO
SAD

Solution in C++11 (judge verdict: Accepted)

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
typedef long long int lli;
const lli inf = 1e18;

vector<lli> shortest_path(int from, vector<vector<pair<int,lli>>> &graph){
    int n = graph.size();
    vector<lli> mincost(n, inf);
    mincost[from] = 0;
    priority_queue<pair<lli,int>> wait;
    wait.push({0LL, from});
    while(!wait.empty()){
        lli cost = -wait.top().first;
        int pos = wait.top().second;
        wait.pop();
        if(cost > mincost[pos]) continue;
        for(auto next: graph[pos]){
            lli ncost = cost + next.second;
            int npos = next.first;
            if(ncost < mincost[npos]){
                mincost[npos] = ncost;
                wait.push({-ncost, npos});
            }
        }
    }
    return mincost;
}

int bridges_dfs(int curr, int prev, int &count,
                vector<int> &pre, vector<int> &low,
                vector<pair<int,int>> &res, vector<vector<int>> &graph){
    if(pre[curr] != -1) return low[curr];
    pre[curr] = count++;
    low[curr] = pre[curr];
    for(int next: graph[curr]){
        if(next == prev) continue;
        int ret = bridges_dfs(next, curr, count, pre, low, res, graph);
        if(ret > pre[curr]){
            res.emplace_back(curr, next);
        }
        low[curr] = min(low[curr], ret);
    }
    return low[curr];
}
vector<pair<int,int>> bridges(vector<vector<int>> &graph){
    int n = graph.size();
    vector<pair<int,int>> res;
    vector<int> pre(n, -1), low(n);
    int count = 0;
    bridges_dfs(0, -1, count, pre, low, res, graph);
    return res;
}
set<pair<int,int>> bridge_list(vector<vector<pair<int,lli>>> &graph, vector<lli> &mincost_s, vector<lli> &mincost_t){
    int n = graph.size();
    vector<vector<int>> adj(n);
    for(int i=0; i<n; i++){
        for(auto next: graph[i]){
            int j = next.first;
            int cost = next.second;
            if(mincost_s[1] == mincost_s[i] +cost +mincost_t[j]){
                adj[i].push_back(j);
                adj[j].push_back(i);
            }
        }
    }
    for(auto &v: adj){
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
    }
    auto ret = bridges(adj);
    set<pair<int, int>> res;
    for(auto p: ret){
        if(mincost_s[p.first] < mincost_s[p.second]){
            res.insert(p);
        }else{
            res.insert({p.second, p.first});
        }
    }
    return res;
}

int main(){
    int n,m;
    cin >> n >> m;
    vector<int> a(m),b(m),c(m);
    map<pair<int,int>, int> minedge, duality;
    vector<vector<pair<int, lli>>> adj(n), adjrev(n);
    for(int i=0; i<m; i++){
        cin >> a[i] >> b[i] >> c[i];
        a[i]--; b[i]--;
        adj[a[i]].emplace_back(b[i], c[i]);
        adjrev[b[i]].emplace_back(a[i], c[i]);
        pair<int, int> pab(a[i], b[i]);
        if(minedge.count(pab) == 0){
            minedge[pab] = c[i];
        }else{
            minedge[pab] = min(minedge[pab], c[i]);
        }
    }
    for(int i=0; i<m; i++){
        pair<int, int> pab(a[i], b[i]);
        if(minedge[pab] == c[i]){
            duality[pab]++;
        }
    }
    int s=0, t=1;
    auto sp_s = shortest_path(s, adj);
    auto sp_t = shortest_path(t, adjrev);
    auto bridge = bridge_list(adj, sp_s, sp_t);
    for(int i=0; i<m; i++){
        pair<int, int> pab(a[i], b[i]);
        if(sp_s[b[i]] +c[i] +sp_t[a[i]] < sp_s[t]){
            cout << "HAPPY" << endl;
        }else if(bridge.find(pab)==bridge.end() or minedge[pab]!=c[i] or duality[pab]>1){
            cout << "SOSO" << endl;
        }else{
            cout << "SAD" << endl;
        }
    }
    return 0;
}